首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios CLLocation didUpdateLocations错误

ios CLLocation didUpdateLocations错误
EN

Stack Overflow用户
提问于 2015-12-29 13:17:07
回答 1查看 153关注 0票数 0

我正在使用XCode7.2版本

我尝试使用locationManager委托函数

代码语言:javascript
复制
 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
 let location = locations.last as! CLLocation
 print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)

 }

但我得到的错误如下:

代码语言:javascript
复制
  Downcast from 'CLLocation?' to 'CLLocation' only unwraps optionals; did you mean to use '!'?

我不知道怎么解决这个问题。

快速代码更改更多

有人能帮我解决吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-29 13:24:17

locations.last返回类型是CLLocation?,这是因为数组可能根本没有元素。作为error状态,您不必将一个可选类型强制转换为非可选类型,只需这样做就可以打开它:

代码语言:javascript
复制
let location = locations.last!

请注意,使用上述方法可能导致异常,并始终考虑使用可选的展开。

代码语言:javascript
复制
if let location = locations.last{
    print("%f/%f",location.coordinate.latitude,location.coordinate.longitude)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34511909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档