Objective c 如何将方法参数标记为由接收方保留
考虑这个对象Objective c 如何将方法参数标记为由接收方保留,objective-c,clang,automatic-ref-counting,Objective C,Clang,Automatic Ref Counting,考虑这个对象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"
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:(\u strong Completion)块中的参数标记为接收方保留的参数。(使用\uuu-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");
}
我不必手动去查漏