14
votes

Hi had lot of trouble in understanding the declaration part in swift programming. I have line of code CLLocationCoordinate2D myCoordinate = myLocation.coordinate; As same as I declared in Swift programming but I'm getting error

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

fatal error: Can't unwrap Optional.None

1
What error are you getting? Where is the declaration of location1? - Gad
I have edited the question ..Can u please check it - Deepak
what is locationManager1? - Kreiri
CLLocationManager @Kreiri - Deepak

1 Answers

23
votes

the location1 can be nil, and you have to check whether or not it is nil before you access to its proprties, like e.g. this:

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...")
}