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. Anyway, I now receive error msg that placeMark is not a member of weakSelf. placeMark is declared as a strong, nonatomic property. 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
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. Something like this:
__weak MyClass weakSelf = self;
Now, you can directly access properties like placeMark
on MyClass
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.