the block would capture a strong reference to self
http://blog.csdn.net/likendsl/article/details/37764813
另一种RAC装B写法的解说
http://www.jianshu.com/p/3d6c4416db5e
weakstrongdance
常看到一个block要使用self,会处理成在外部声明一个weak变量指向self,在block里又声明一个strong变量指向weakSelf:
__weak __typeof(self)weakSelf = self;
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
}];weakSelf是为了block不持有self,避免循环引用,而再声明一个strongSelf是因为一旦进入block执行,就不允许self在这个执行过程中释放。block执行完后这个strongSelf会自动释放,没有循环引用问题。
可以用以下的宏定义
#ifndef weakify
#if __has_feature(objc_arc)
#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")
#else
#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")
#endif
#endif
#ifndef strongify
#if __has_feature(objc_arc)
#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")
#else
#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")
#endif
#endif 相关推荐
zzjmay 2020-02-20
Cloudeep 2020-01-10
nuligannima 2013-06-26
jackalwb 2019-12-24
jackalwb 2019-12-15
hlfsunshine 2013-09-02
84334052 2019-11-19
xiaobaichen 2019-11-05
jiangtie 2019-11-02
Elmo 2019-10-27
IOSPanPan 2015-04-15
ice00 2019-08-04
yexiekai 2012-02-26
sufwei 2014-02-23
chermonlove 2019-08-02
atb 2013-06-26
caifengguo 2018-06-14
tonygsw 2014-11-30