簡體   English   中英

ios CLLocation didUpdateLocations錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM