[英]iOS - CLLocation Manager didUpdateLocations called too much time
[英]ios CLLocation didUpdateLocations error
我正在使用XCode7.2版本
我嘗試使用locationManager委托函數
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last as! CLLocation
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}
但是我得到以下錯誤:
Downcast from 'CLLocation?' to 'CLLocation' only unwraps optionals; did you mean to use '!'?
我不知道如何解決這個問題。
快速代碼更改更多
有誰可以幫助我解決問題?
謝謝
locations.last
返回類型是CLLocation?
那就是因為數組可能根本沒有任何元素。 作為錯誤狀態,當您只需通過以下操作將其解包時,就不必將可選類型強制轉換為非可選類型:
let location = locations.last!
請注意,使用上述方法可能會導致異常,請務必考慮使用可選的拆包
if let location = locations.last{
print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.