1

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)

        }
     }

})
}
random_0620
  • 1,636
  • 5
  • 23
  • 44

1 Answers1

1

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)
}
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
  • It worked! Thanks! Isn't that pretty tedious to create a whole new location variable? Im not really liking this whole optionals thing with swift. – random_0620 Jan 02 '15 at 15:52
  • For the first one I am getting an error that says Bound value in a conditional binding must be optional type – random_0620 Jan 02 '15 at 15:55
  • It is a bit tedious, but only because it's forcing you to check something that, without optionals, you should also check – that is, that the result that came back from `getLocationDataFromString` is valid (it might not be), and that the array actually has a first element (it might not). Without optionals, you could ignore these possibilities _but that would be a bad idea and lead to runtime crashes_. Optionals just force you to do the right thing. Of course, if the context means you can be certain the optional contains a value, you can just skip the unwrap with `!`. But 99/100 you shouldn't. – Airspeed Velocity Jan 02 '15 at 16:03
  • See [this answer](http://stackoverflow.com/a/27623568/3925941) for a longer explanation... – Airspeed Velocity Jan 02 '15 at 16:04
  • "For the first one I am getting an error that says Bound value in a conditional binding must be optional type" You get that when you have a non-optional type on the rhs of the `=`. Given `location` is definitely an optional (you declare it as such right above) I suspect a typo somewhere. – Airspeed Velocity Jan 02 '15 at 16:16
  • I have this: ` weather.getLocationDataFromString("California USA", completion: { (location:[CLLocationCoordinate2D]?,error:NSError?) -> (Void) in if let location = location { if let coordinate = location[0] as CLLocationCoordinate2D { println(coordinate.latitude) } } }) ` and still getting an error. I copied directly from your code – random_0620 Jan 02 '15 at 16:26
  • The problem is with `location[0] as CLLocationCoordinate2D `. `as` without a question mark means "don't argue, this is an x". `as?` means, "if this is an x, give me it in an optional, otherwise give me nil". So because you are using `as` rather than `as?`, the result isn't optional so the `if let` binding is barfing. Bear in mind array subscripts don't return optionals – so if you call array[0] on an empty array, you get a runtime exception. That's why `first` is useful – it returns an optional in case the array is empty. – Airspeed Velocity Jan 02 '15 at 16:30