使用copy
修饰属性网络
想不循环应用,那么在 block 外面这样声明异步
__weak __typeof(self)weakSelf = self;
接着,在 block 里面这样async
__strong __typeof(weakSelf)strongSelf = weakSelf;
这样既防止循环应用,又避免 block 内部 self 会无效的可能优化
AFNetworking
里面这样使用的不少atom
UITableView
的优化最基本的,cell 重用机制,若是这个都不知道的话,那能够去撞墙了。。。spa
异步加载数据,这个也是很基本的线程
自动载入更新数据,好比每次载入20条信息,而后在滚动到最后5条信息,就加载更多的信息code
图片下载完成之后,判断 cell 若是是可见的,那么就须要更新图像队列
异步请求,我只认准 GCD | GCD异步,你值得拥有 (广告先走一波)图片
网络请求放在子线程,UI 只能在主线程更新
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { // 处理各类耗时间的事情,好比网络请求数据,天知道要何时才能结束 dispatch_async(dispatch_get_main_queue(), ^(void) { // 好了之后,咱们回到主线程进行界面刷新 }); });
只执行一次
Xcode 自带的代码块
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ });
延迟执行
Xcode 自带的代码块
double delaySeconds = 5.0; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delaySeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ });
自定义队列
dispatch_queue_t baidu_queue = dispatch_queue_create("baidu.com", NULL); dispatch_async(baidu_queue, ^{ });
访问沙盒里面几个路径
//获取根目录 NSString *homePath = NSHomeDirectory(); NSLog(@"Home目录:%@",homePath);
//获取Documents文件夹目录 NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsPath = [docPath objectAtIndex:0]; NSLog(@"Documents目录:%@",documentsPath);
//获取Cache目录 NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cacPath objectAtIndex:0]; NSLog(@"Cache目录:%@",cachePath);
//获取Library目录 NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libPath = [libsPath objectAtIndex:0]; NSLog(@"Library目录:%@",libPath);
//获取temp目录 NSString *tempPath = NSTemporaryDirectory(); NSLog(@"temp目录:%@",tempPath);
写入与读取
NSArray *testArray1 = @[@"1",@"2"]; //documentsPath是前面建立的 NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testArray1.text"]; //写入 [testArray1 writeToFile:filePath atomically:YES]; //读取 NSArray *readTestArray1 = [NSArray arrayWithContentsOfFile:filePath];
文件管理
//获取文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //文件目录名 NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"ttttest"]; //执行建立 [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
在刚才建立的文件夹下继续写入内容
//在文件目录下继续写入的路径 NSString *tPath = [testDirectory stringByAppendingPathComponent:@"t1.txt"]; //要写入内容 NSString *contentString = @"ready..."; //执行写入 [fileManager createFileAtPath:tPath contents:[contentString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
获取一个目录下面全部的子文件
//获取documents下面的全部文件,能够看到隐藏的内容 NSArray *file = [fileManager subpathsOfDirectoryAtPath:documentsPath error:nil]; NSLog(@"file%@",file); NSArray *file2 = [fileManager subpathsAtPath:documentsPath]; NSLog(@"file2=%@",file2);
改变文件管理器所能操做的位置
//移到准备操做的目录下 [fileManager changeCurrentDirectoryPath:[documentsPath stringByExpandingTildeInPath]]; //建立文件 NSString *fileName = @"newTest.txt"; NSString *tString = @"ttttttt"; [fileManager createFileAtPath:fileName contents:[tString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
删除文件
//接着上面,就一句话 [fileManager removeItemAtPath:fileName error:nil];