1、传入block的函数,注意各个return分支,都要block()
1 2 3 4 5 6 7 8 9 |
- (void)POSTwithURL:(NSString *)urlString completionHandler:(void(^)())handler { if (...) { handler(); } else if (....) { handler(); } else { handler(); } } |
如果某种情况忘了回调,调用方可能就会一直等待回调。特别是多个异步函数嵌套,分支复杂时;
2、这种情况会死锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [self POSTwithURL:@"http://www.baidu.com" completionHandler:^{ dispatch_semaphore_t semaphore1 = dispatch_semaphore_create(0); //信号量嵌套时,这里的参数要不同,如dispatch_semaphore_create(1) [self POSTwithURL:@"http://www.sina.com" completionHandler:^{ dispatch_semaphore_signal(semaphore1); }]; dispatch_semaphore_wait(semaphore1,DISPATCH_TIME_FOREVER); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER); |
3、成员属性要写对
1 |
@property (nonatomic, readonly, copy) NSString *name; |
发生过误把copy 写成assign,导致读name时的野指针;
野指针是try-catch不到的!!!
4、注意编译选项
声明NSArray *names;默认配置会把它初始成nil;但总有人会改这个配置的;
5、给别人写闭源框架时,做参数检查,能多详细就多详细
想让接入方传入一个成员为NSNumber的数组a,判断传空的条件有a==nil, a. count == 0, a[0] ==0等等校验。
6 componentsSeparatedByString有坑
1 2 3 4 5 |
NSString *s = @""; NSArray *a = [s componentsSeparatedByString:@"|"]; //a的结果是@[@""],并不是nil,也不是@[] |
7 给iOS10开发notification service extension,用来推带图片的推送,调试service Target时总是“unable to attach”。困扰许久,发现根工具的architecture被改成了仅armv7,而主target还是atmv7、arm64。结果生成的notification service extension的architecture也是仅armv7