简体   繁体   English

__弱的自我无法正常工作

[英]__weak self not working as expected

I have the following code: 我有以下代码:

    __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);

}

the reason I want to use a weak self, its because I saw this in another example when I was researching why "Xcode couldn't find" my properties by self.some_property inside this block. 我之所以要使用弱自我,是因为我在研究为什么在此块内通过self.some_property无法“找到Xcode”我的属性时,在另一个示例中看到了这一点。 Anyway, I now receive error msg that placeMark is not a member of weakSelf. 无论如何,我现在收到错误消息msg,表明placeMark不是softSelf的成员。 placeMark is declared as a strong, nonatomic property. placeMark被声明为强大的非原子属性。 Any help appreciated 任何帮助表示赞赏

尝试转换变量,因为这里只有一个(id),因此Xcode无法识别实例的自定义属性

__weak typeof(self) weakSelf = self;

try this 尝试这个

 __weak typeof(<self class name>) weakSelf = self;

it is a proper way to create your weakSelf object to use in the block 这是创建在该块中使用的weakSelf对象的正确方法

you can also create another class object to use in the block like 您还可以创建另一个类对象以在块中使用

__weak typeof(A) weakA = self;
__weak typeof(B) weakB = objB;

If you are sure on the object type then replace id with the actual object type here. 如果您确定对象类型,请在此处将id替换为实际的对象类型。 Something like this: 像这样:

__weak MyClass weakSelf = self;

Now, you can directly access properties like placeMark on MyClass . 现在,您可以直接在MyClass上访问诸如placeMark属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM