0

我的代码:

-(void)addressLocation:(NSArray *)inputData {
NSString *plusAddress = [[inputData objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,%@&sensor=false&region=pl", plusAddress, [inputData objectAtIndex:1]]];

__block CLLocationCoordinate2D newRecord;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSArray *results = [JSON valueForKey:@"results"];
    NSNumber *latNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lat"];
    NSNumber *lngNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lng"];
    CGFloat lat = [latNumber floatValue];
    CGFloat lng = [lngNumber floatValue];
    newRecord = CLLocationCoordinate2DMake(lat, lng);
}  failure:nil];
[operation start];
[operation waitUntilFinished];

KantorPoint *newPoint = [[KantorPoint alloc] init];
newPoint.coordinate = newRecord;
newPoint.name = [inputData objectAtIndex:1];
newPoint.address = [inputData objectAtIndex:0];
[self.pointersArray addObject:newPoint];
}

我正在尝试newRecord从操作块传输,如答案所示。但是我不能做类似内部块newPoint.coordinate的事情(得到一个错误)。有没有办法解决这个问题?nullreturn newRecord

4

1 回答 1

0

我不确定您的目标,但我会以这种方式重写代码。

- (void)addressLocation:(NSArray *)inputData {

    NSString *plusAddress = [[inputData objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,%@&sensor=false&region=pl", plusAddress, [inputData objectAtIndex:1]]];

    __block YourClass *selfBlock = self; // __weak YourClass *selfBlock = self; if you use ARC

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSArray *results = [JSON valueForKey:@"results"];
        NSNumber *latNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lat"];
        NSNumber *lngNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lng"];
        CGFloat lat = [latNumber floatValue];
        CGFloat lng = [lngNumber floatValue];
        CLLocationCoordinate2D newRecord = CLLocationCoordinate2DMake(lat, lng);

        KantorPoint *newPoint = [[KantorPoint alloc] init];
        newPoint.coordinate = newRecord;
        newPoint.name = [inputData objectAtIndex:1];
        newPoint.address = [inputData objectAtIndex:0];
        [selfBlock.pointersArray addObject:newPoint];

    }  failure:nil];

    // the queue needs to be allocated somewhere...
    [[self operationQueue] addOperation:operation];
}

如果您使用 ARC:__block不会阻止对象被保留,请注意。改为使用__weak

编辑

基于phix23,您需要在某处保留您的操作。因此,一种简单的方法是创建一个类型的属性NSOperationQueue并将操作添加到该队列。当您将其添加到队列中时,您无需执行start该操作。

希望有帮助。

于 2012-07-08T14:34:27.060 回答