addressDictionary 在 iOS 11.0 中首先被弃用

addressDictionary is deprecated first deprecated in iOS 11.0

刚开始研究 IOS11,将我的代码从 IOS10.3 移动到 IOS11 导致了以下挑战,而以下代码在 IOS11:

    CLGeocoder *GeoCoder = [[CLGeocoder alloc] init];
[GeoCoder reverseGeocodeLocation:_LocationManager.location completionHandler:^(NSArray *PlaceMarks, NSError *Error)
 {
     if (!Error)
     {
         MKPlacemark *PlaceMark_MK;

         if (PlaceMarks.count > 0)
         {
             MKPlacemark *PlaceMark = [PlaceMarks objectAtIndex:0];

             if (PlaceMark.location.coordinate.latitude != 0.0F && PlaceMark.location.coordinate.longitude != 0.0F)
             {
                 PlaceMark_MK = [[MKPlacemark alloc] initWithCoordinate:PlaceMark.location.coordinate addressDictionary:PlaceMark.addressDictionary];

.....

现在使用 IOS11,它抱怨 PlaceMark.addressDictionary,而 addressDictionary 已弃用。

addressDictionary' is deprecated: first deprecated in iOS 11.0 - Use @properties

我尝试这样做,但不幸的是,InitWithCoordinate 函数无论如何都需要 AddressDictionary。

不知道有没有人遇到过同样的问题,我知道它很新(IOS11 昨天刚出来)。

我想我还是找到了答案:

我替换了以下行:

PlaceMark_MK = [[MKPlacemark alloc] initWithCoordinate:PlaceMark.location.coordinate addressDictionary:PlaceMark.addressDictionary];

使用以下行:

PlaceMark_MK = [[MKPlacemark alloc] initWithCoordinate:PlaceMark.location.coordinate postalAddress:PlaceMark.postalAddress];

这似乎解决了这个问题。

您需要通过如下属性访问

    var placemark = placemarks[0] as? CLPlacemark
    var address = "\(placemark?.thoroughfare ?? ""), \(placemark?.locality ?? ""), \(placemark?.subLocality ?? ""), \(placemark?.administrativeArea ?? ""), \(placemark?.postalCode ?? ""), \(placemark?.country ?? "")"
        print("\(address)")