视图控制器的生命周期
#pragma mark 视图控制器的生命周期
#pragma mark - 1
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"1---viewVillAppera");
}
#pragma mark - 2
-(void)viewDidAppear:(BOOL)animated {
NSLog(@"2---didAppear");
}
#pragma mark - 3
- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"3---viewWillDisappera");
}
#pragma mark -4
- (void)viewDidDisappear:(BOOL)animated {
NSLog(@"4---viewDidDisappear");
}
present的页面跳转方式
[self presentViewController:[[SecondViewController alloc]init] animated:YES completion:NULL];
property关键字
为成员属性自动生成setter_getter方法
@property 属性类型 属性名;
Xcode打印出日志信息(Swift)
ebugPrint("test")
print("test")
CFShow("test" as CFTypeRef!)
获取iOS的应用信息(Swift)
let mainbudle = Bundle.main//或得当前对象可执行文件所在的目录
let appIdentifier = mainbudle.bundleIdentifier//获取应用程序的标识符
let info = mainbudle.infoDictionary//程序包的配置信息
let bundleName = mainbudle.object(forInfoDictionaryKey: "CFBundleName")//获取应用的名称
let version = mainbudle.object(forInfoDictionaryKey: "CFBundleShortVersionString")//获得程序的版本号
获取系统的所有字体(Swift)
for family in UIFont.familyNames
{
for font in UIFont.fontNames(forFamilyName: family)
{
print(font)
}
}
获取屏幕的点击次数(Swift)
UITouch *touch = [touches anyObject];
if (touch.tapCount==1){
NSLog(@"单次点击");
}
else if (touch.tapCount==2){
NSLog(@"双击");
}
NSURLConnect网络连接
-
首先在头部引入两个代理
//实现两个代理协议 <NSURLConnectionDelegate,NSURLConnectionDataDelegate> { //定义一个URL连接对象 NSURLConnection *_urlConnect; NSMutableData *_mutabeData;//创建一个可变的二进制数据对象 }
-
创建请求和实现请求
-(void)pressBtn { NSString *urlStr = @"https://www.baidu.com"; NSURL *url = [NSURL URLWithString:urlStr];//将字符串转换成URL NSURLRequest *request = [NSURLRequest requestWithURL:url];//创建请求 _urlConnect = [NSURLConnection connectionWithRequest:request delegate:self];//进行网络请求 _mutabeData = [[NSMutableData alloc]init]; }
-
实现需要的代理方法
//处理错误信息的代理方法
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error is %@",error);
}
//处理服务器返回的响应码
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//将响应码转换为http响应码
NSHTTPURLResponse *res = (NSHTTPURLResponse*) response;
if (res.statusCode==200)
{
NSLog(@"连接成功");
}
else if (res.statusCode==404)
{
NSLog(@"无法加载网页");
}
else if (res.statusCode==500)
{
NSLog(@"服务器宕机或关机");
}
}
//接收回传的数据的时候进行调用
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//将每次接收的数据连接起来
[_mutabeData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//将二进制数据_mutableData转换为字符串
NSString *str = [[NSString alloc]initWithData:_mutabeData encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
}
自定义转场动画
//定义一个动画对象
CATransition* animate = [CATransition animation];
//设置动画的时间
animate.duration = 1;
//设置动画的类型,决定动画的效果
animate.type = @"cube";
//设置动画的子类型,决定动画的方向等
animate.subtype = kCATransitionFromRight;
//设置动画的轨迹模式
animate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
//添加动画到视图层
[self.navigationController.view.layer addAnimation:animate forKey:nil];
VCSecond *vcSecond = [[VCSecond alloc]init];
[self.navigationController pushViewController:vcSecond animated:YES];
NSUserDefaults数据存储
-(void)pressWrite
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//useDefaults只能存储能够文件化的数据,无法存储视图,图片之类的文件
[ud setObject:@"Felx" forKey:@"name"];
NSNumber *num = [NSNumber numberWithInt:100];
[ud setObject:num forKey:@"num"];
//创建一个可文件化的数组
NSArray *array = [NSArray arrayWithObjects:@"1",@"3",@"5",@"7",nil];
[ud setObject:array forKey:@"array"];
}
-(void)pressRead
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; id object = [ud objectForKey:@"array"];
NSString *name = object;
NSLog(@"name is %@",name);
}
多线程之GCD
- GCD使用的是C而不是OC,所以字符串前不需要加@符号。
- GCD自动管理线程的生命周期。
- 任务的执行方式分为同步执行和异步执行任务两种方式。
- 同步和异步的区别在于:同步任务不开启新的线程,异步任务则开启新的线程。
- 并发队列可以让多个任务同时执行。
- 串行队列只能让任务一个一个的一次执行。
- 并发与串行是任务执行的两种方式。
并发队列+同步任务
//创建并发队列
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
//向队列添加同步任务
dispatch_sync(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---run---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 10:20:59.795 GCD_OC[9563:341711] ---run---<NSThread: 0x600000066240>{number = 1, name = main}
number=1表明当前线程是主线程;
并发队列 + 线程同步任务不会开启新的线程(或者说创建新的线程对象)。
并发队列+异步任务
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i=0;i<2;i++){
NSLog(@"---异步1----%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0;i<2;i++){
NSLog(@"---异步2----%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:05:21.116 GCD_OC[10192:372041] ---异步2----<NSThread: 0x60800007e440>{number = 4, name = (null)}
2017-03-27 11:05:21.116 GCD_OC[10192:371920] ---异步1----<NSThread: 0x60000007cac0>{number = 3, name = (null)}
2017-03-27 11:05:21.116 GCD_OC[10192:372041] ---异步2----<NSThread: 0x60800007e440>{number = 4, name = (null)}
2017-03-27 11:05:21.116 GCD_OC[10192:371920] ---异步1----<NSThread: 0x60000007cac0>{number = 3, name = (null)}
全局队列+同步任务
/*
参数一为队列的优先级:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 高
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) 低
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 后台
*/
//获取全局队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_sync(queue, ^{
for (int i=0;i<2; i++) {
NSLog(@"---同步1---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i=0;i<2; i++) {
NSLog(@"---同步2---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:08:22.723 GCD_OC[10239:374355] ---同步1---<NSThread: 0x608000061a80>{number = 1, name = main}
2017-03-27 11:08:22.724 GCD_OC[10239:374355] ---同步1---<NSThread: 0x608000061a80>{number = 1, name = main}
2017-03-27 11:08:22.724 GCD_OC[10239:374355] ---同步2---<NSThread: 0x608000061a80>{number = 1, name = main}
2017-03-27 11:08:22.725 GCD_OC[10239:374355] ---同步2---<NSThread: 0x608000061a80>{number = 1, name = main}
全局队列+异步任务
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---异步1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---异步2---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:11:29.228 GCD_OC[10300:377161] ---异步2---<NSThread: 0x6080000703c0>{number = 4, name = (null)}
2017-03-27 11:11:29.228 GCD_OC[10300:377162] ---异步1---<NSThread: 0x600000075ac0>{number = 3, name = (null)}
2017-03-27 11:11:29.228 GCD_OC[10300:377161] ---异步2---<NSThread: 0x6080000703c0>{number = 4, name = (null)}
2017-03-27 11:11:29.228 GCD_OC[10300:377162] ---异步1---<NSThread: 0x600000075ac0>{number = 3, name = (null)}
串行队列+同步任务
//第二个参数设置为DISPATCH_QUEUE_SERIAL或NULL表示创建的队列为串行队列
dispatch_queue_t queue = dispatch_queue_create("queue", NULL);
dispatch_sync(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---同步1---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---同步2---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:17:33.036 GCD_OC[10391:381684] ---同步1---<NSThread: 0x608000078580>{number = 1, name = main}
2017-03-27 11:17:33.037 GCD_OC[10391:381684] ---同步1---<NSThread: 0x608000078580>{number = 1, name = main}
2017-03-27 11:17:33.038 GCD_OC[10391:381684] ---同步2---<NSThread: 0x608000078580>{number = 1, name = main}
2017-03-27 11:17:33.038 GCD_OC[10391:381684] ---同步2---<NSThread: 0x608000078580>{number = 1, name = main}
串行队列+异步任务
//第二个参数设置为DISPATCH_QUEUE_SERIAL或NULL表示创建的队列为串行队列
dispatch_queue_t queue = dispatch_queue_create("queue", NULL);
dispatch_async(queue, ^{
for (int i=0;i<2; i++) {
NSLog(@"---异步1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0;i<2; i++) {
NSLog(@"---异步2---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:21:13.755 GCD_OC[10465:385017] ---异步1---<NSThread: 0x608000072780>{number = 3, name = (null)}
2017-03-27 11:21:13.756 GCD_OC[10465:385017] ---异步1---<NSThread: 0x608000072780>{number = 3, name = (null)}
2017-03-27 11:21:13.757 GCD_OC[10465:385017] ---异步2---<NSThread: 0x608000072780>{number = 3, name = (null)}
2017-03-27 11:21:13.758 GCD_OC[10465:385017] ---异步2---<NSThread: 0x608000072780>{number = 3, name = (null)}
主队列+异步任务
//获取主队列
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---异步1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0;i<5; i++) {
NSLog(@"---异步2---%@",[NSThread currentThread]);
}
});
终端输出:
2017-03-27 11:24:08.535 GCD_OC[10518:387519] ---异步1---<NSThread: 0x60800007e480>{number = 1, name = main}
2017-03-27 11:24:08.536 GCD_OC[10518:387519] ---异步1---<NSThread: 0x60800007e480>{number = 1, name = main}
2017-03-27 11:24:08.536 GCD_OC[10518:387519] ---异步2---<NSThread: 0x60800007e480>{number = 1, name = main}
2017-03-27 11:24:08.536 GCD_OC[10518:387519] ---异步2---<NSThread: 0x60800007e480>{number = 1, name = main}
主队列+同步任务:会造成死锁的现象
多线程是NSOeraiotn
NSOperation实现的三种方式:
- NSInvocationOperation;
- NSBlockOperation;
- 自定义Operation。
NSInvocationOperation
-(void)invocationoperation{
NSInvocationOperation * op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operation) object:nil];
[op start];
}
-(void)operation{
NSLog(@"---1----%@",[NSThread currentThread]);
}
终端输出:
2017-03-27 16:12:34.153 NSOperation_OC[13348:521902] ---1----<NSThread: 0x60000007a940>{number = 1, name = main}
NSBlockOperation
-(void)blockOperation {
NSBlockOperation *bop = [NSBlockOperation blockOperationWithBlock:^{
//始终在主线程中完成
NSLog(@"---2----%@",[NSThread currentThread]);
}];
//添加的额外任务在子线程中完成
[bop addExecutionBlock:^{
NSLog(@"---3----%@",[NSThread currentThread]);
}];
[bop addExecutionBlock:^{
//设置子线程的名称
[NSThread currentThread].name = @"子线程";
NSLog(@"---4----%@",[NSThread currentThread]);
}];
[bop start];
}
终端输出:
2017-03-27 16:12:34.154 NSOperation_OC[13348:521902] ---2----<NSThread: 0x60000007a940>{number = 1, name = main}
2017-03-27 16:12:34.154 NSOperation_OC[13348:521997] ---3----<NSThread: 0x608000262040>{number = 3, name = (null)}
2017-03-27 16:12:34.154 NSOperation_OC[13348:521995] ---4----<NSThread: 0x608000262080>{number = 4, name = 子线程}
自定义Operation
自定义一个NSOperation继承自NSOperation
#import "CustomOperation.h"
@implementation CustomOperation
//自定义的NSOperation需要重写main方法
-(void)main {
NSLog(@"---1---%@",[NSThread currentThread]);
}
@end
使用自定义的Operation
-(void)customOperation{
//实例化一个自定义的CustomOperation对象
CustomOperation *cop = [[CustomOperation alloc]init];
[cop start];
}
网友评论