Note that there are some explanatory texts on larger screens.

plurals
  1. POObj-C: __block variables
    primarykey
    data
    text
    <p>Is it possible to assign a local variable a value whose scope is outside a block and have it retain its value? In particular, I'm coding for iOS and I have a nested block inside another blocks, and i want to assign a NSString a value inside the block a value and later (outside the blocks) use it. I tried using __block nut when i refer to the NSString after the blocks i get a bad access error. I am using ARC is that matters. For example:</p> <pre><code>__block NSString *str; someBlock ^(id param1) { str = @"iPhone"; } [str getCharAtIndex:1]; //or w/e </code></pre> <p>Am i doin something conceptually wrong or this not allowed or what? Help is much appreciated.</p> <p>Edit:</p> <p>here's the actual code, basically the code gets the the tweet as a json object then all I'm tring to do is display the text. in the code i haven't extracted the text from the json, i was trying to do a proof of concept</p> <pre><code>- (IBAction)getTweet:(id)sender { __block NSString *displayStr; //account instance ACAccountStore *store = [[ACAccountStore alloc] init]; ACAccountType *twitterAcountType = [store accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierTwitter]; //request access [store requestAccessToAccountsWithType: twitterAcountType withCompletionHandler: ^(BOOL granted, NSError *error) { if (!granted) { //display error on textView } else { //get available accounts NSArray *twitterAccounts = [store accountsWithAccountType: twitterAcountType]; if([twitterAccounts count] &gt; 0) { //get first account ACAccount *account = [twitterAccounts objectAtIndex: 0]; ////make authenticated request to twitter //set-up params NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; [params setObject:@"1" forKey:@"include_entities"]; [params setObject:@"1" forKey:@"count"]; //which REST thing to call NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"]; //create request TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET]; //attach account info [request setAccount: account]; [request performRequestWithHandler: ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if(error != nil) { //display error } else { NSError *jsonError; NSArray *timeline = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableLeaves error: &amp;jsonError]; if (jsonError == nil) { /////////////////////////// ///heres the src of error// /////////////////////////// //display data NSLog(@"array: %@", timeline); displayStr = @"whats the deal with this"; //i tried this but i think ARC takes care of this [displayStr retain]; } else { //display error } } }];//end block de request } else { //display error } } }];//end block de store ///////then heres where i get the bad access error [self.lastTweetText setText:displayStr]; }//end getTweet </code></pre> <p>also thanks for the help guys</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload