DevilInDisguise
DevilInDisguise

Reputation: 360

__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. 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

Upvotes: 0

Views: 331

Answers (3)

Abhinav
Abhinav

Reputation: 38152

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.

Upvotes: 0

JAY RAPARKA
JAY RAPARKA

Reputation: 1326

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;

Upvotes: 0

Lory Huz
Lory Huz

Reputation: 1508

Try to cast your variable because here you just have an (id), so Xcode doesn't recognize the custom properties of your instance

__weak typeof(self) weakSelf = self;

Upvotes: 1

Related Questions