【问题标题】:addressDictionary is deprecated: first deprecated in iOS 11.0 - Use @propertiesaddressDictionary 已弃用:首先在 iOS 11.0 中弃用 - 使用 @properties
【发布时间】:2018-06-07 19:42:00
【问题描述】:

我正在与我一起获取位置和地址。因为我成功获取了位置,但在获取地址时我收到了类似警告,

'addressDictionary' 已弃用:首先在 iOS 11.0 中弃用 - 使用 @properties

有什么解决办法吗...

【问题讨论】:

  • 错误是不言自明的,不要使用带有硬编码键的字典,而是使用 CLPlacemark 的属性。另外,你为什么要得到一个字符串,然后用第一个字符串的内容分配另一个字符串,然后将第三个字符串和第二个字符串的内容副本放在其他地方?
  • 是的,你说得对。最后我使用了这些属性。

标签: ios objective-c cocoa-touch core-location


【解决方案1】:

在 Swift 5 中

//let location: CLLocation = CLLocation(latitude: 16.511131, longitude: 80.658725)//Convert lat & lng in to CLLocation

    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location) { (placemarksArray, error) in
        print(placemarksArray!)
        if (error) == nil {
            if placemarksArray!.count > 0 {
                let placemark = placemarksArray?[0]
                let address = "\(placemark?.subThoroughfare ?? ""), \(placemark?.thoroughfare ?? ""), \(placemark?.locality ?? ""), \(placemark?.subLocality ?? ""), \(placemark?.administrativeArea ?? ""), \(placemark?.postalCode ?? ""), \(placemark?.country ?? "")"
                print("\(address)")
            }
        }

    }

结果喜欢:

**print(placemarksArray!) :** [20/19, Road Number 19, 20/19, Road Number 19, Wadala West, Mumbai, 400031, Maharashtra, India @ <+19.01761470,+72.85616440> +/- 100.00m, region CLCircularRegion (identifier:'<+19.01732600,+72.85634600> radius 70.52', center:<+19.01732600,+72.85634600>, radius:70.52m)]

**print("\(address)") :** 20/19, Road Number 19, Mumbai, Wadala West, Maharashtra, 400031, India

在目标 C 中

if (!(error))
    {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];    
        NSString *address = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@",
                             placemark.thoroughfare,
                             placemark.locality,
                             placemark.subLocality, 
                             placemark.administrativeArea, 
                             placemark.postalCode,
                             placemark.country];
        NSLog(@"%@", address);

    }

【讨论】:

    【解决方案2】:

    快速回答 - 旧方式已注释掉:

    let street = placemark.thoroughfare! // addressDictionary!["Street"] as? String ?? " "
        let city =  placemark.subAdministrativeArea! // addressDictionary!["City"] as? String ?? " "
        let state = placemark.administrativeArea!//addressDictionary!["State"] as? String ?? " "
        let zip =  placemark.isoCountryCode!// addressDictionary!["ZIP"] as? String ?? " "
        let country = placemark.country! // addressDictionary!["Country"] as? String ?? " "
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多