[CLLocationCoordinate2d]?没有名为下标的成员

[CLLocationCoordinate2d]? does not have a member named subscript

我正在尝试从数组第一个元素中的 CLLocationCOordinate2d 中获取纬度和经度。 if let saying [CLLocationCoordinate2d] 时出现错误?没有名为下标的成员。有任何想法吗?谢谢!

    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)

        }
     }

})
}

它是可选的,所以你需要打开它。您已经在检查 nil,所以您快到了:

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

或者,也许更好,如果您只需要第一个元素:

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