ios - 为什么我会收到此错误 : Result values in '? :' expression have mismatching types '[String]?' and 'String'

标签 ios iphone swift google-maps ipad

所以我是 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/

相关文章:

ios - 将 base 64 转换为 NSString

iphone - Facebook SDK : How to i get the photo properties after posting an image?

iphone - viewDidLoad 在 initWithFrame 之前被调用?

ios - 进入后台、复制新文本并返回应用程序后,如何从 UIPasteboard 检索文本?

swift - 如何将单个字符串发送到 Apple Watch

ios - 快速调用中的额外参数 'completion'

c# - 没有互联网连接的iOS应用崩溃

iphone - 如果我的 navigationController 返回 "(null)"我该怎么办?

ios - Swift 中的 Xcode : navigation bar not showing in UI collection view (simulator)

swift - 使用 Alamofire 的 iOS http POST