ios - swift 将 CLLocation 转换为 CLLocationCoordinate2D

标签 ios swift

Hi 在理解 swift 编程中的声明部分时遇到了很多麻烦。 我有一行代码 CLLocationCoordinate2D myCoordinate = myLocation.coordinate; 与我在 Swift 编程中声明的一样,但出现错误

var location1:CLLocation! = locationManager1.location
var coordinate1:CLLocationCoordinate2D = location1.coordinate

fatal error :无法展开 Optional.None

最佳答案

location1 可以是nil,你必须在访问它的属性之前检查它是否是nil,例如这个:

let locationManager1: CLLocationManager // your location manager

// ...

if let location1: CLLocation = locationManager1.location {
    var coordinate1: CLLocationCoordinate2D = location1.coordinate

    // ... proceed with the location and coordintes

} else {
    println("no location...")
}

关于ios - swift 将 CLLocation 转换为 CLLocationCoordinate2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363911/

相关文章:

swift - 什么是元组和列表?

ios - 在 TableView 的 realm 上显示 resultArray

iphone - 关于对象方法调用的通知

ios - 移除 NSNotificationCenter 观察者

ios - 如何观察表格 View 中的对象数量?

ios - 如何取消特定 View 的通知中心视觉效果 (iOS)?

ios - 在当前事件的 UIViewController 中呈现 UIAlertController

ios - UIPageControl 不滑动

ios - 无法导入AFHTTPRequestOperationLogger

ios - 更改导航栏高度