我试图从位于数组第一个元素中的CLLocationCOordinate2d中获取纬度和经度。如果说[CLLocationCoordinate2d],我收到错误?没有名为subscript的成员。有任何想法吗?谢谢!
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)
}
}
})
}
答案 0 :(得分:1)
它是可选的,所以你需要打开它。您已经在检查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)
}