![](/img/trans.png)
[英]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.