[英]The [weak self] is giving an error for the following code: Expected ',' Separator
[英]__weak self not working as expected
我有以下代码:
__weak id weakSelf = self;
[geocoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if(error)
NSLog(@"Geocoder failed with error: %@", error);
else
weakSelf.placeMark = [placemarks objectAtIndex:0];
}];
NSLog(@"current placemark: %@", self.placeMark);
}
我之所以要使用弱自我,是因为我在研究为什么在此块内通过self.some_property无法“找到Xcode”我的属性时,在另一个示例中看到了这一点。 无论如何,我现在收到错误消息msg,表明placeMark不是softSelf的成员。 placeMark被声明为强大的非原子属性。 任何帮助表示赞赏
尝试转换变量,因为这里只有一个(id),因此Xcode无法识别实例的自定义属性
__weak typeof(self) weakSelf = self;
尝试这个
__weak typeof(<self class name>) weakSelf = self;
这是创建在该块中使用的weakSelf对象的正确方法
您还可以创建另一个类对象以在块中使用
__weak typeof(A) weakA = self;
__weak typeof(B) weakB = objB;
如果您确定对象类型,请在此处将id
替换为实际的对象类型。 像这样:
__weak MyClass weakSelf = self;
现在,您可以直接在MyClass
上访问诸如placeMark
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.