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

  • Home
  • SEARCH
  • 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 8060801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:06:47+00:00 2026-06-05T10:06:47+00:00

I’m baffled as to why I cannot access my global variable again after it

  • 0

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

  • 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-05T10:06:48+00:00Added an answer on June 5, 2026 at 10:06 am

    Okie doke, sounds like a couple of issues (one, none or all may apply, it’s hard to tell within the context you’ve provided):

    1 – “But I just wind up getting empty strings when I am accessing the variables outside of the block.”

    Depends when you’re accessing them outside the block. This is because there is no guarantee that latitude and longitude will have been populated at the time of querying them. The block provided is a completion handler for CLGeocoder; it will be called when the geocoder has found locations of interest. Being able to retrieve location data, and to do a search based on that location takes time, and the NSLog statement straight after has a high probability of being called before any ‘placemarks’ have been found.

    2 – *”IOS5 __block variable throws EXC_BAD_ACCESS outside scope”*

    You are assigning the following in the block:

    latitude = [NSString stringWithFormat:@"%f", latLong.coordinate.latitude];
    longitude = [NSString stringWithFormat:@"%f", latLong.coordinate.longitude];
    

    stringWithFormat is a method that returns an autoreleased value, but you’re not retaining them anywhere. If you’re not using ARC (see https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html for ARC in memory management terms, and http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html generally), then this will cause issues when accessing the values elsewhere, as you’re accessing an object that no longer exists. If you are using ARC, then this shouldn’t be a problem, as the variables will be strong by default, and retain the value for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.