所以我是 swift 的新手,我正在尝试使用谷歌地图。 我无法弄清楚为什么这条线有效:
让 state = p?.administrativeArea != nil ? p?.administrativeArea : "nil6"
但不是这一行:
让 areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : "nil7"
我在 areaOfInterest 行收到此错误消息:
'? 中的结果值:' 表达式有不匹配的类型 '[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!)"
}
}
}
最佳答案
这是因为您正在访问的地标的所有其他属性都是字符串,感兴趣的区域除外,它是一个(可选的)字符串数组。当它无法工作时,您正试图施放它。
如果你真的想像那样格式化你的错误,你可以使用:
让 areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : ["nil7"]
关于ios - 为什么我会收到此错误 : Result values in '? :' expression have mismatching types '[String]?' and 'String' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33473136/