random0620
random0620

Reputation: 1686

[CLLocationCoordinate2d]? does not have a member named subscript

I am trying to get the latitude and longitude out of a CLLocationCOordinate2d that is in the first element of the array. I am getting an error with if let saying [CLLocationCoordinate2d]? does not have a member named subscript. Any ideas? Thanks!

    override func viewDidLoad() {
    super.viewDidLoad()

    weather.getLocationDataFromString("California USA", completion: { (location:[CLLocationCoordinate2D]?,error:NSError?) -> (Void) in
        if location != nil{
            if let coordinate = location[0] as CLLocationCoordinate2D{ // ERROR: [CLLocationCoordinate2d]? does not have a member named subscript
                println(coordinate.latitude)

        }
     }

})
}

Upvotes: 1

Views: 1524

Answers (1)

Airspeed Velocity
Airspeed Velocity

Reputation: 40955

It's an optional, so you need to unwrap it. You're already checking for nil so you're almost there:

if let location = location {
    if let coordinate = location[0] as CLLocationCoordinate2D {
        println(coordinate.latitude)
    }
}

Or, perhaps nicer, if all you want is the first element:

if let coordinate = location?.first as? CLLocationCoordinate2D {
    println(coordinate.latitude)
}

Upvotes: 1

Related Questions