I'm baffled as to why I cannot access my global variable again after it has been through a block. Here is my code:
__block NSString *latitude;
__block NSString *longitude;
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:location completionHandler:^(NSArray* placemarks, NSError* error)
{
for (CLPlacemark* aPlacemark in placemarks)
{
CLLocation *latLong = aPlacemark.location;
latitude = [NSString stringWithFormat:@"%f", latLong.coordinate.latitude];
longitude = [NSString stringWithFormat:@"%f", latLong.coordinate.longitude];
//works fine
NSLog(@"CLLOCATION SSSSSSSSSSSSSSSSSSSSSS LAT: %@, LONG: %@", latitude, longitude);
}
}];
//no bueno
NSLog(@"CLLOCATION SSSSSSSSSSSSSSSSSSSSSS LAT: %@, LONG: %@", latitude, longitude);
Now I've tried initializing my NSString
s in different ways:
__block NSString *latitude = @"";
__block NSString *longitude = @"";
and:
__block NSMutableString *latitude = [NSMutableString string];
__block NSMutableString *longitude = [NSMutableString string];
But I just wind up getting empty strings when I am accessing the variables outside of the block.
This is particularly baffling because in Apple's documentation http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1
, they're able to set variables outside of blocks, use them, and retrieve them just fine.
<code>
tags; highlight the code and select the button that looks like{}
on top of the text field. – CodaFi