0

I'm struggling with getting a variable out of a block. Seems to be a pretty basic thing but I can't figure it out! How can I access it? e.g. usercity out of the block? Usercity is declared as NSString in .h.

[ceo reverseGeocodeLocation: loc completionHandler:
 ^(NSArray *placemarks, NSError *error) {


     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     //NSLog(@"placemark %@",placemark);
     //String to hold address
     //NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
     //NSLog(@"addressDictionary %@", placemark.addressDictionary);

     //NSLog(@"placemark %@",placemark.region);
     NSLog(@"Land %@",placemark.country);  // Give Country Name
     NSLog(@"City %@",placemark.locality); // Extract the city name
     NSLog(@"Adresse %@",placemark.name);
     //NSLog(@"location %@",placemark.ocean);
     NSLog(@"Zip %@",placemark.postalCode); //ZipCode
     NSLog(@"sonstiges %@",placemark.subLocality);


     //Save values in variables
     usercountry = placemark.country;
     usercity = placemark.locality;
     userzip = placemark.postalCode;
     NSLog(@"usercity: %@",usercity);

     //NSLog(@"location %@",placemark.location);



 }

 ];
hlfcoding
  • 2,532
  • 1
  • 21
  • 25
tyler
  • 424
  • 4
  • 16

2 Answers2

1

Is this what you're looking for?

__block NSString *userCity;
[ceo reverseGeocodeLocation: loc completionHandler:^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     ...
     userCity = placemark.locality;

}];

But if you want to actually be able to check its value outside of the block, you'll have to do so after the completion handler updates the value. Perhaps make it a property, ie. self.userCity?

hlfcoding
  • 2,532
  • 1
  • 21
  • 25
  • I made it a property. Like self.userCity=placemark.locality. But when I print the property after the block it´s still null. I don´t really understand blocks but I only want to get the city of the user available in my project. Not only in the block. When I print my city AFTER the block like "NSLog("City of User. %@", self.usercity); And even though INSIDE the block in the same way. The print AFTER the block gets printed first. And secondly the print INSIDE the block. Like the other way round. So in this case I can imagine that the block was not running when I printed the city outside the block. – tyler Oct 19 '14 at 20:04
  • 1
    You should probably learn more about blocks. The short version is the method you're passing that block to executes it asynchronously (later), after the rest of your code running at this time. – hlfcoding Oct 19 '14 at 20:06
  • I'm not sure what level you are, but here are general explanations of what asynchronous programming is: http://en.wikipedia.org/wiki/Asynchronous_method_invocation, http://en.wikipedia.org/wiki/Callback_(computer_programming). Also, if you need to do something after you get the placemark in the completion handler, like updating UI, you'll just need to do it in the completion handler. – hlfcoding Oct 19 '14 at 20:13
  • I think this is way over my level. Sorry I cannot figure it out. Could you provide some code? I´m stuck on this problem since 2 days. What I simply want is to get the city as a normal variable in my project. So for example I can access/print it in another method. Heelp! Im totaly lost... – tyler Oct 19 '14 at 20:20
0

Your code in the block has to store usercity where you want it. You can't "get something out" of a block, the code in the block has to do it.

You do know that a block can access all variables in the surrounding method, don't you?

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Yes I know that it can access the variables around it. But when I print usercity outside of the block anywhere in my class it´s null?! – tyler Oct 19 '14 at 08:33