简体   繁体   English

Objective-C:__block不起作用

[英]Objective-C: __block not working

I could not figure out how to change the value of results inside the success block. 我无法弄清楚如何更改成功块内results的值。 I use __block like some post suggests but results is forever nil. 我像某些帖子所建议的那样使用__blockresults永远是零。 I set breakpoint inside of block and make sure that JSON is not nil, which download data as I expected. 我在块内设置了断点,并确保JSON不为零,这可以按预期下载数据。

I am using AFNetworking library if that's relevant. 如果相关,我正在使用AFNetworking库。

+(NSArray *)eventsByCityID:(NSString *)cityID startIndex:(NSUInteger)start count:(NSUInteger)count
{
    __block NSArray *results = nil;

    [[DoubanHTTPClient sharedClient] getPath:@"event/list" parameters:@{@"loc":dataSingleton.cityID} success:^(AFHTTPRequestOperation *operation, id JSON) {
        results = [JSON valueForKey:@"events"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"download events error: %@ \n\n",error);
    }];

    return results;
}

More likely than not, that [very poorly named] method getPath:parameters:success:failure: is asynchronous. [非常糟糕的名称]方法getPath:parameters:success:failure:很有可能是异步的。

Thus, you need to tell something in the success block that the value has changed. 因此,您需要在成功块中告知某些内容该值已更改。 Ie

^{
     [something yoManGotEvents:[JSON valueForKey:@"events"]];
 }

(Methods shouldn't be prefixed with get outside of very special circumstances. Third party libraries with lots of API using that prefix outside of said circumstances raise question as to what other system specific patterns they may not be following.) (在非常特殊的情况下,方法不应以get为前缀。在上述情况之外,具有大量使用该前缀的API的第三方库会提出有关它们可能不遵循的其他系统特定模式的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM