__block variable returning nil after block runs [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-04 06:56:30

问题


I have a created a string that I have prefixed with the __block identifier so that I am able to access that variable outside of my block. However once I attempt to run the application the variable returns 'nil':

-(void)downloadParcelData {
    __block NSString *test;

    MKMapRect mRect = self.mapView.visibleMapRect;
    NSArray *array = [NSArray array];
    array = [self getBoundingBox:mRect];

    NSString *polygonString = [self convertCoordinates:array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = nil;

    NSDictionary *parameters = @{@"client" : @"xxxxxxxxxx",
                             @"spatial_intersect" : [NSString    stringWithFormat:@"POLYGON((%@))", [polygonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]]};

    [manager GET:@"someURL" parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        test = responseObject[@"txn_id"];
     } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
     }];

    [self fetchParcelData:test];

}

Any ideas on why this may be happening? Thanks in advance.


回答1:


[manager GET:parameters:progress:success:failure] appears to be an async method. That line does not block; execution immediately continues to [self fetchParcelData:]. The success block is where you should put code you want to handle the response to the GET request.



来源:https://stackoverflow.com/questions/42605841/block-variable-returning-nil-after-block-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!