Obj-C:__block变量 [英] Obj-C: __block variables

查看:88
本文介绍了Obj-C:__block变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为局部变量分配一个范围在块之外且保留其值的值?特别是,我正在为iOS编码,我在另一个块中有一个嵌套块,我想在块中为一个NSString赋值一个值,然后(在块之外)使用它。我尝试使用__block nut,当我在块之后引用NSString时出现错误的访问错误。我使用ARC是重要的。例如:

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:

__block NSString *str;

someBlock ^(id param1)
{
    str = @"iPhone";
}

[str getCharAtIndex:1]; //or w/e

我做了什么概念错误或不允许或什么?非常感谢帮助。

Am i doin something conceptually wrong or this not allowed or what? Help is much appreciated.

编辑:

这是实际代码,基本上代码获取推文为一个json对象,然后我要做的就是显示文本。在代码中我没有从json中提取文本,我试图做一个概念证明

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

- (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] > 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: &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

还要感谢帮助人员

推荐答案

首先, str 只会在阻止后更新被执行。因此,除非您在该行使用 dispatch_sync ,否则在此行: [str getCharAtIndex:1]; 该块是不太可能被执行, str 将不会更新。

First, str will get updated only after the block is executed. So unless you are using dispatch_sync for that block otherwise at this line:[str getCharAtIndex:1]; the block is unlikely to be executed and str will not get updated.

其次,__block变量不会被块自动保留对象,如果您不使用ARC。这意味着如果你没有保留它,那么当你访问 str 时, str 可能是一个解除分配的对象,崩溃你的应用程序。

Second, __block variable will not automatic retained by the block object if you are not using ARC. This means if you are not retain it, than by the time you accessing str, str may be a deallocated object and crash your app.

这篇关于Obj-C:__block变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆