Swift 2.0 - 无法为“[CLPlacemark]”类型的值添加下标?
Swift 2.0 - Cannot Subscript a value of type '[CLPlacemark]?'
我正在尝试将我的 swift 1.2 应用程序转换为 2.0。我已经尝试了一些我在互联网上找到的修复程序,但它不起作用。
错误:Converting to Swift 2.0 - Cannot Subscript a value of type '[CLPlacemark]?'
代码:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
print("Error: " + error!.localizedDescription)
return
}
if placemarks!.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
print("Error with the data.")
}
})
}
已解决:
替换:
let pm = placemarks[0] as! CLPlacemark
对于:
let pm = placemarks?[0]
或更新 Swift 2.0 安全:
guard let currentPlacemarks = placemarks where currentPlacemarks.count > 0 else {
// error handling here
return
}
guard let pm = currentPlacemarks[0] where pm is CLPlacemark else {
// more error handling
return
}
self.displayLocationInfo(pm)
我正在尝试将我的 swift 1.2 应用程序转换为 2.0。我已经尝试了一些我在互联网上找到的修复程序,但它不起作用。
错误:Converting to Swift 2.0 - Cannot Subscript a value of type '[CLPlacemark]?'
代码:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
print("Error: " + error!.localizedDescription)
return
}
if placemarks!.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
print("Error with the data.")
}
})
}
已解决:
替换:
let pm = placemarks[0] as! CLPlacemark
对于:
let pm = placemarks?[0]
或更新 Swift 2.0 安全:
guard let currentPlacemarks = placemarks where currentPlacemarks.count > 0 else {
// error handling here
return
}
guard let pm = currentPlacemarks[0] where pm is CLPlacemark else {
// more error handling
return
}
self.displayLocationInfo(pm)