Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:10:14+00:00 2026-06-03T21:10:14+00:00

I don’t think I understand how blocks work exactly in this scenario. I’m trying

  • 0

I don’t think I understand how blocks work exactly in this scenario. I’m trying to get the location from CLGeocoder and save the MKPlacemark after the block is finished. So in this method:

- (MKPlacemark *)placeMarkFromString:(NSString *)address {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    __block MKPlacemark *place;
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        [placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSLog(@"%@", [obj description]);
        }];

        // Check for returned placemarks
        if (placemarks && [placemarks count] > 0) {
            CLPlacemark *topResult = [placemarks objectAtIndex:0];

            // Create an MKPlacemark and add it to the mapView
            place = [[MKPlacemark alloc] initWithPlacemark:topResult];
            [self.mapView addAnnotation:place];
        }

        if (error) {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
    }];
    NSLog(@"%@", [place description]);
    return place;
}

When I run my code, the MKPlacemark place does get added to the map. However, if I log the value, it is NULL. I think that might be because the block doesn’t get executed right away right? So my NSLog might be executed first, and then the completionHandler runs. However, how would I return the MKPlacemark from this method so I can use that value elsewhere in my code? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:10:16+00:00Added an answer on June 3, 2026 at 9:10 pm

    If you want to retain “place” create an ivar/property on self (whatever self is) for it. declare it as a __block variable. Then do:

    self.place = [[MKPlacemark alloc] initWithPlacemark:topResult];

    Or you could create an NSArray ivar if you wanted to keep track of multiple places and then just add the object to the array each time the block executes.

    And yes, your NSLog(@”%@”, [place description]); will run before the block executes.

    EDIT: If you want to “return” place from this method you’d need to make your method a block method something like this:

    typedef void (^SuccessBlock)(id);
    typedef void (^FailureBlock)(NSError *);
    
    - (void)placeMarkFromString:(NSString *)address withSuccess:(SuccessBlock)success andFailure:(FailureBlock)failure {
    
        ... //inside the enumerateObjectsUsingBlock block
        success(place);
    
        ...
        failure(error);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know how to explain it better but i'm trying to get a response
Don't think that I'm mad, I understand how php works! That being said. I
Don't quite understand why this copy constructor is not invoked when I build with
Don't think my virtualhost is working correctly. This is what I have inside of
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't think this is a repost, difficult to search for the word between because
don't know better title for this, but here's my code. I have class user
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't understand the meaning of $:<< . in Ruby. I upgraded Ruby to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.