我尝试将起点和终点本地化为地址字符串,以便将其存储到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
爱因德

问题是我的代码没有等待CLGeocoder完成,因此返回时未设置我的变量returnAddress,并且它为空。

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

最佳答案

因为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.
}

或者,您可以在reverseGeocodeLocation完成后使用NSNotificationCenter发送通知。您可以在其他需要的地方订阅这些通知,并从那里完成逻辑。将[self remainderOfMethodHereWithReturnAddress:returnAddress];替换为:
NSDictionary *infoToBeSentInNotification = [NSDictionary dictionaryWithObject:returnAddress forKey:@"returnAddress"];

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

Here's使用NSNotificationCenter的示例。

10-07 12:38