无法指定'[String]类型的值?'输入'String?'

时间:2017-10-30 14:02:22

标签: swift

这是var 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))
            }
        }
    }

这是地方类,这是我想要添加它的customTableViewCell的功能

func update(place:QPlace) {
        myLabel.text = place.getDescription()
        myImage.image = nil
        myLabel2.text = place.vicinity
        myLabel3.text = place.types     

1 个答案:

答案 0 :(得分:1)

var types: [String]?String的可选数组; myLabel3.text的值是可选的StringString

您需要从数组中获取值或加入值以设置标签文本,例如:

myLabel3.text = place.types?.joined()