【问题标题】:Why am I getting Cannot convert value of type Bool to expected argument type String为什么我无法将 Bool 类型的值转换为预期的参数类型 String
【发布时间】:2017-10-21 05:23:57
【问题描述】:

出现几个“无法将 Bool 类型的值转换为预期的参数类型 String”的错误。编码方法需要一个字符串,但它得到一个布尔值?

这里是代码。错误见附图。

import Foundation

class Restaurant {
    var name = ""
    var item = ""
    var location = ""
    var image = ""
    var isVisited = false
    var phone = ""
    var rating = ""

    init(name: String, item: String, location: String, phone: String, image: String, isVisited: Bool) {
        self.name = name
        self.item = item
        self.location = location
        self.phone = phone
        self.image = image
        self.isVisited = isVisited
    }

    class func makeNewsItem(_ notificationDictionary: [String: AnyObject]) -> Restaurant? {
        if let name = notificationDictionary["name"] as? String,
            let phone = notificationDictionary["phone"] as? String,
            let location = notificationDictionary["location"] as? String {
            let date = Date()
            let image = ""
            let visited = false
            let item = ""

            let newsItem = Restaurant(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited)

            NotificationCenter.default.post(name: Notification.Name(rawValue: RestaurantTableViewController.RefreshNewsFeedNotification), object: self)
            return newsItem
        }
        return nil
    }
}

extension Restaurant: NSCoding {
    struct CodingKeys {
        static var Name = "name"
        static var Item = "item"
        static var Location = "location"
        static var Image = "image"
        static var IsVisited:Bool = false
        static var Phone = "phone"
        static var Rating = "rating"
    }

    convenience init?(coder aDecoder: NSCoder) {
        if let name = aDecoder.decodeObject(forKey: CodingKeys.Name) as? String,
            let location = aDecoder.decodeObject(forKey: CodingKeys.Location) as? Date,
            let phone = aDecoder.decodeObject(forKey: CodingKeys.Phone) as? String {

            let date = Date()
            let image = aDecoder.decodeObject(forKey: CodingKeys.Image) as? String
            let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String
            let item = aDecoder.decodeObject(forKey: CodingKeys.Item) as? String

            self.init(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited)
        } else {
            return nil
        }
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: CodingKeys.Name)
        aCoder.encode(location, forKey: CodingKeys.Location)
        aCoder.encode(phone, forKey: CodingKeys.Phone)
        aCoder.encode(item, forKey: CodingKeys.Item)
        aCoder.encode(image, forKey: CodingKeys.Image)
        aCoder.encode(isVisited, forKey: CodingKeys.IsVisited)
        aCoder.encode(rating, forKey: CodingKeys.Rating)
    }
}

【问题讨论】:

    标签: swift


    【解决方案1】:

    您不能将布尔值添加到 forKey。这必须是一个字符串值,因此将其更改为:

    aCoder.encode(isVisited, forKey: CodingKeys.IsVisited)
    

    收件人:

    aCoder.encode(isVisited, forKey: "IsVisited")
    

    同样适用于:

    let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String
    

    收件人:

    let visited:Bool = aDecoder.decodeBool(forKey: "IsVisited") // note, no need for as? String here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2016-01-14
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多