1

我正在尝试将起点和终点本地化为地址字符串,以便可以将其存储到NSUserDefaults. 问题是该方法继续执行并且没有设置我的变量。

NSLog(@"Begin");

__block NSString *returnAddress = @"";

[self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
    if(error){
        NSLog(@"%@", [error localizedDescription]);
    }

    CLPlacemark *placemark = [placemarks lastObject];

    startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                          placemark.subThoroughfare, placemark.thoroughfare,
                          placemark.postalCode, placemark.locality,
                          placemark.administrativeArea,
                          placemark.country];
    returnAddress = startAddressString;

    //[self.view setUserInteractionEnabled:YES];
}];
NSLog(returnAddress);

NSLog(@"Einde");

这是我的应用程序调试器显示的内容:

开始
_

例如,如果我的地址是:“Mainstreet 32​​, CITY”。那么我想看到的是以下内容:

开始
Mainstreet 32​​, CITY
Einde

问题是我的代码没有等待我CLGeocoder完成,所以我的变量returnAddress在返回时没有设置,它是空的。

有谁知道如何解决这个问题?

4

1 回答 1

6

因为reverseGeocodeLocation有一个完成块,所以当执行到达它时它被移交给另一个线程 - 但主线程上的执行仍将继续到下一个操作,即NSLog(returnAddress). 此时,returnAddress尚未设置,因为reverseGeocodeLocation刚刚移交给另一个线程。

使用完成块时,您必须开始考虑异步工作。

考虑作为方法中的最后一个操作离开reverseGeocodeLocation,然后使用完成块内的其余逻辑调用新方法。这将确保在您获得returnAddress.

- (void)someMethodYouCall 
{
    NSLog(@"Begin");
    __block NSString *returnAddress = @"";

    [self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
        if(error){
            NSLog(@"%@", [error localizedDescription]);
        }

        CLPlacemark *placemark = [placemarks lastObject];

        startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                              placemark.subThoroughfare, placemark.thoroughfare,
                              placemark.postalCode, placemark.locality,
                              placemark.administrativeArea,
                              placemark.country];
        returnAddress = startAddressString;

        //[self.view setUserInteractionEnabled:YES];

        NSLog(returnAddress);
        NSLog(@"Einde");

        // call a method to execute the rest of the logic 
        [self remainderOfMethodHereUsingReturnAddress:returnAddress];
    }];
// make sure you don't perform any operations after reverseGeocodeLocation.
// this will ensure that nothing else will be executed in this thread, and that the
// sequence of operations now follows through the completion block.
}

- (void)remainderOfMethodHereUsingReturnAddress:(NSString*)returnAddress {
   // do things with returnAddress.
}

或者您可以使用 NSNotificationCenter 在完成时发送通知reverseGeocodeLocation。您可以在其他任何需要的地方订阅这些通知,并从那里完成逻辑。替换[self remainderOfMethodHereWithReturnAddress:returnAddress];为:

NSDictionary *infoToBeSentInNotification = [NSDictionary dictionaryWithObject:returnAddress forKey:@"returnAddress"];

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"NameOfNotificationHere" 
    object:self
    userInfo: infoToBeSentInNotification];
    }];

是使用 NSNotificationCenter 的示例。

于 2013-01-15T20:53:21.340 回答