Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocation to CLLocationCoordinate2D in swift

Tags:

ios

swift

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

like image 864
Deepak Avatar asked Jun 23 '14 10:06

Deepak


1 Answers

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...")
}
like image 146
holex Avatar answered Oct 02 '22 13:10

holex