How to get formatted address NSString from AddressDictionary?

前端 未结 9 1412
旧巷少年郎
旧巷少年郎 2020-12-17 14:58

Trying to get formatted address from AddressDictionary, that I got from CLGeocoder. Used following code with no result:

subtitle = [NSString stringWithString         


        
9条回答
  •  再見小時候
    2020-12-17 15:35

    Simply create extension for CLLocation:

    typealias AddressDictionaryHandler = ([String: Any]?) -> Void
    
    extension CLLocation {
    
        func addressDictionary(completion: @escaping AddressDictionaryHandler) {
    
            CLGeocoder().reverseGeocodeLocation(self) { placemarks, _ in
                completion(placemarks?.first?.addressDictionary as? [String: AnyObject])
            }
        }
    }
    

    Example:

    let location = CLLocation()
    
    location.addressDictionary { dictionary in
    
        let city = dictionary?["City"] as? String
        let street = dictionary?["Street"] as? String
    }
    

提交回复
热议问题