2014-06-23 30 views
8

您在理解swift編程中的聲明部分時有許多麻煩。 我的代碼行CLLocationCoordinate2D myCoordinate = myLocation.coordinate; 作爲同我在斯威夫特編程聲明,但我得到的錯誤CLLocation位於CLLocationCoordinate2D在swift中

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

致命錯誤:無法解開Optional.None

+0

你會得到什麼錯誤? 「location1」的聲明在哪裏? – Gad

+0

我編輯了這個問題..你可以檢查一下 – Deepak

+0

'locationManager1'是什麼? – Kreiri

回答

14

location1可以nil,和在訪問它的proprities之前,你必須檢查它是否是nil,比如eg這個:

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...") 
} 
相關問題