技巧1:
__weak typeof(self)weakSelf=self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong typeof(weakSelf)strongSelf=weakSelf;
[strongSelf doSomething];
});
weakSelf是为了block不持有self,避免循环引用,而再声明一个strongSelf是因为一旦进入block执行,就不允许self在这个执行过程中释放。block执行完后这个strongSelf会自动释放,没有循环引用问题。
技巧2 查找字符串是否包含 另一字符串
NSString *string = @"1234567890987654321qwertyuiop";
NSRange range1 = [string rangeOfString:@"rty"];
if (range1.length > 0) {
NSLog(@"{字符串中“rty”的位置,长度}==%@",NSStringFromRange(range1));
}
//判断在一串字符串中是否找到某个字符串
NSRange range2 = [string rangeOfString:@"abc"];
if (range2.location != NSNotFound) {
NSLog(@"找到了@“abc”这个字符串!");
}else{
NSLog(@"没找到!");
}