Why am I getting this error : Result values in '? :' expression have mismatching types '[String]?' and 'String'

Why am I getting this error : Result values in '? :' expression have mismatching types '[String]?' and 'String'

所以我是 swift 的新手,我正在试验 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"]

尝试使用 ["nil7"] 而不是 "nil7"