Swift 无法分配类型为';[字符串]?';输入';字符串?';
这是varSwift 无法分配类型为';[字符串]?';输入';字符串?';,swift,Swift,这是varvar-types:[String]?这里我得到了这个错误myLabel3.text=place.types我如何调整它?我查看了其他类似的问题,但没有发现与我的问题相同的问题 import UIKit import CoreLocation private let geometryKey = "geometry" private let locationKey = "location" private let latitudeKey = "lat" private let long
var-types:[String]?
这里我得到了这个错误myLabel3.text=place.types
我如何调整它?我查看了其他类似的问题,但没有发现与我的问题相同的问题
import UIKit
import CoreLocation
private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let openingHoursKey = "opening_hours"
private let openNowKey = "open_now"
private let vicinityKey = "vicinity"
private let typesKey = "types"
private let photosKey = "photos"
class QPlace: NSObject {
var location: CLLocationCoordinate2D?
var name: String?
var photos: [QPhoto]?
var vicinity: String?
var isOpen: Bool?
var types: [String]?
init(placeInfo:[String: Any]) {
// coordinates
if let g = placeInfo[geometryKey] as? [String:Any] {
if let l = g[locationKey] as? [String:Double] {
if let lat = l[latitudeKey], let lng = l[longitudeKey] {
location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
}
}
}
// name
name = placeInfo[nameKey] as? String
// opening hours
if let oh = placeInfo[openingHoursKey] as? [String:Any] {
if let on = oh[openNowKey] as? Bool {
isOpen = on
}
}
// vicinity
vicinity = placeInfo[vicinityKey] as? String
// types
types = placeInfo[typesKey] as? [String]
// photos
photos = [QPhoto]()
if let ps = placeInfo[photosKey] as? [[String:Any]] {
for p in ps {
photos?.append(QPhoto.init(photoInfo: p))
}
}
}
这是place类,这是我要添加它的customTableViewCell的函数
func update(place:QPlace) {
myLabel.text = place.getDescription()
myImage.image = nil
myLabel2.text = place.vicinity
myLabel3.text = place.types
var-types:[String]?
是String
的可选数组;myLabel3.text
的值是可选的String
,或String
为了设置标签文本,您需要从数组中获取一个值或加入这些值,例如:
myLabel3.text = place.types?.joined()
您正在将字符串数组添加到采用字符串的label属性中?请详细说明您打算执行的操作。根据你的问题,我假设类型是一个字符串数组,但place.types是什么并不清楚。请仔细阅读错误消息
[String]
是一种集合类型(数组)和String
单个对象。Swift的强类型系统不支持这种不匹配。您必须展平阵列或选择一个特定的项目以assign@Jeet我编辑了错误中提到的问题,types
是一个字符串数组,您可以将其分配给一个字符串(myLabel3.text
)!你的目标是什么?