設置地址字符串我嘗試本地化開始和結束點到地址字符串,以便我可以將它存儲到NSUserDefaults
。問題是該方法繼續執行,並沒有設置我的變量。使用reverseGeocodeLocation設置地址字符串並從方法
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");
這是我的應用程序調試器顯示:
開始
einde
舉例來說,如果我的位置的地址是: 「主街32 CITY」。那麼我想看到的是以下內容:
開始
主街32,CITY
Einde
的問題是,我的代碼不會等待我的CLGeocoder
來完成,所以我變量returnAddress
在返回時未設置,並且爲空。
有誰知道如何解決這個問題?
是的,我知道,但在我的情況下,我需要在完成塊外使用該變量。因爲我在塊之外還有其他一些邏輯,所以我不能放在塊內。你知道一個辦法嗎?你有沒有鏈接或樣本我的異步線程?謝謝 – Wesley
我已經用一些方法更新了答案,您可以用您需要的返回地址執行剩餘的邏輯。 –
太棒了,我已經想通了!謝謝。 – Wesley