您在了解swift编程中的声明部分时遇到了很多麻烦。
我有一行代码CLLocationCoordinate2D myCoordinate = myLocation.coordinate;
和我在Swift编程中声明的一样,但我得到了错误
var location1:CLLocation! = locationManager1.location
var coordinate1:CLLocationCoordinate2D = location1.coordinate
致命错误:无法打开Optional.None
答案 0 :(得分:21)
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...")
}