0xS*_*ina 6 cocoa-touch objective-c ios
这段代码:
__weak VeryCool *weakSelf = self;
something.changeHandler = ^(NSUInteger newIndex) {
if (newIndex == 0) {
[weakSelf.options removeObjectForKey:@"seller"];
}
};
Run Code Online (Sandbox Code Playgroud)
给我一个警告,找不到财产选择.这是真的,因为选项是一个ivar,但没有声明为属性.是否有可能以某种方式从weakSelf获取选项而不将其声明为属性?
Ken*_*ses 23
如需直接进入ivar,请使用->.例如:
__weak VeryCool *weakSelf = self;
something.changeHandler = ^(NSUInteger newIndex) {
if (newIndex == 0) {
VeryCool* strongSelf = weakSelf;
if (strongSelf)
[strongSelf->options removeObjectForKey:@"seller"];
}
};
Run Code Online (Sandbox Code Playgroud)
重要的是要检查strongSelf是否为非nil因为对nil指针实例变量的直接访问会崩溃(这与使用nil接收器调用方法不同,属性访问只是方法调用).
| 归档时间: |
|
| 查看次数: |
3955 次 |
| 最近记录: |