【问题标题】:Swift 5 reverseGeocodeLocation() result in deprecated addressDictionarySwift 5 reverseGeocodeLocation() 导致不推荐使用的 addressDictionary
【发布时间】:2020-09-24 09:28:22
【问题描述】:

我正在使用CLGeoCoder 实例通过其方法reverseGeocodeLocation() 获取给定坐标集(纬度和经度)的城市和国家/地区。

文档指定此数据应位于 CLPlacemarkcountrylocality 属性中,但结果似乎仅位于自 iOS 11 以来已弃用的 addressDictionary 属性中。

Image of debugging instance showing addressDictionary present but neither country nor locality

因此这有效,但在addressDictionary 的所有三种用法中显示此警告: 'addressDictionary' was deprecated in iOS 11.0: Use @properties

let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, preferredLocale: nil) { (placemarksArray, error) in
    if let error = error {
        print(error)
    } else {
        if let placemark = placemarksArray?.last {
            var city = ""
            if let ct = placemark.addressDictionary?["City"] {
                city = "\(ct), "
            }
            var state = ""
            if let st = placemark.addressDictionary?["State"] {
                state = "\(st), "
            }
            var country = ""
            if let cot = placemark.addressDictionary?["Country"] {
                country = "\(cot)"
            }
            self.cityCountry = "\(city)\(state)\(country)"
        }
    }
}

我尝试引用属性,但它们只返回 nil 值。

是我调用了错误的方法还是这是一个错误?

【问题讨论】:

标签: ios swift clgeocoder clplacemark


【解决方案1】:

突然对countrylocalitysubAdministrativeArea 的引用开始起作用了!但无法弄清楚是什么原因导致它们不能更早地工作。

【讨论】:

  • 您好,欢迎来到 Stack Overflow!如果您自己找到了问题的答案,请将其标记为已接受。这样它就不会再出现在未答复的列表中了。干杯!
猜你喜欢
  • 2019-08-18
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 2019-08-18
  • 2012-02-09
相关资源
最近更新 更多