因此,我是新手,我正在尝试使用Google地图。
我不知道为什么这行有效:
let state = p?.administrativeArea!= nil? p?.administrativeArea:“nil6”
但不是这一行:
让areaOfInterest = p?.areasOfInterest!= nil? p?.areasOfInterest:“nil7”
我在areaOfInterest行上收到此错误消息:
结果值以'? :'表达式的类型'[String]不匹配?'和'String'
提前致谢。
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil
{
print(error)
}
else
{
let p = placemarks?[0]
print(p)
let subThoroughfare = p?.subThoroughfare != nil ? p?.subThoroughfare : "nil1"
let thoroughfare = p?.thoroughfare != nil ? p?.thoroughfare : "nil2"
let country = p?.country != nil ? p?.country : "nil3"
let postal = p?.postalCode != nil ? p?.postalCode : "nil4"
let city = p?.locality != nil ? p?.locality : "nil5"
let state = p?.administrativeArea != nil ? p?.administrativeArea : "nil6"
let areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : "nil7"
self.addressLabel.text = "\(subThoroughfare!) \(thoroughfare!) \n \(city!), \(state!) \n \(country!) \(postal!)"
}
}
}
最佳答案
这是因为您要访问的所有其他地标属性都是字符串,但感兴趣的区域除外,该区域是一个(可选)字符串数组。您正在尝试在无法正常运行时对其进行投射。
如果您确实要像这样格式化错误,则可以使用:let areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : ["nil7"]