我想寫一個函數來反轉地理編碼的位置並將結果字符串分配給一個變量。繼this後我得到了這樣的事情:無法從CLGeocoder返回字符串reverseGeocodeLocation
extension CLLocation {
func reverseGeocodeLocation(completion: (answer: String?) -> Void) {
CLGeocoder().reverseGeocodeLocation(self) {
if let error = $1 {
print("[ERROR] \(error.localizedDescription)")
return
}
if let a = $0?.last {
guard let streetName = a.thoroughfare,
let postal = a.postalCode,
let city = a.locality else { return }
completion(answer: "[\(streetName), \(postal) \(city)]")
}
}
}
}
對於調用這個函數我剛剛拿到了這樣的事情:
location.reverseGeocodeLocation { answer in
print(answer)
}
而是我想要的answer
字符串值賦給一個變量,我不知道如何從關閉中傳遞數據。做這種事情的最佳方式是什麼?
You posted回答,而我正在撰寫我的。你在幾秒鐘之內擊敗了我...... :) –
哈哈。抱歉。也許堆棧溢出應該有一個功能,你知道,如果有人正在輸入一個答案(有點像Skype或一些聊天平臺)... – Rob
謝謝!解決了問題unlike️ –