考虑此对象Widget
typedef void (^Completion)(BOOL result);
@interface Widget ()
@property (nonatomic,strong) Completion retainedBlock;
@end
@implementation Widget
-(void)doStuff:(Completion)block {
NSLog(@"stuff done , caller will still release");
block(YES);
}
-(void)doRetainingStuff:(__strong Completion)block {
NSLog(@"stuff done - mwah hah i have retained you! NO releasing!");
self.retainedBlock = block;
self.retainedBlock(YES);
}
@end
如何将-(void)doRetainingStuff:(__strong Completion)block
中的参数标记为接收方保留的参数。 (使用__strong
并不这样做)
如果传入的块包含对self
的引用,则将保留调用者并获得保留周期。
我希望clang告诉我,我在深度或浅度模式下都有保留周期。
是的,我知道在主叫方面可能会变弱,但是我想将合同正式化,以便我的编译器告诉我什么时候是愚蠢的。
所以当我这样做时......
- (IBAction)releasableCall:(id)sender {
NSLog(@"i will still release");
[self.widget doStuff:^(BOOL result) {
[self zap];
}];
}
- (IBAction)nonReleaseableCall:(id)sender {
NSLog(@"i will not release after this");
[self.widget doRetainingStuff:^(BOOL result) {
[self zap];
}];
}
-(void)zap {
NSLog(@"foo");
}
我不必手动进行泄漏狩猎。