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.coordinatenullreturn 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 に答える