swift - 无法将类型 'String.Type' 的值转换为预期的参数类型 'String'

标签 swift

swift 4/Xcode 9.3/OS X 10.13.4/iOS 11.3 & 11.2.6

我正在尝试构建我的应用程序,但收到上述错误消息。我一遍又一遍地检查代码,但我无法弄清楚为什么会出现此错误。我不确定您需要查看代码的哪一部分,但这是我遇到错误的页面。错误代码标记了最后一行代码。

import UIKit
import os.log

class Bonus: NSObject, NSCoding {

    //MARK: Archiving Paths
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("bonuses")

    //MARK: Properties
    var bonusCode: String
    var category: String
    var name: String
    var value: Int
    var city: String
    var state: String
    var photo: UIImage?

    //MARK: Initialization
    init?(bonusCode: String, category: String, name: String, value: Int, city: String, state: String, photo: UIImage?) {

        // The name must not be empty.
        guard !name.isEmpty else {
            return nil
        }

        // The value must not be negative.
        guard (value >= 0) else {
            return nil
        }

        // Initialize stored properties.
        self.bonusCode = bonusCode
        self.category = category
        self.name = name
        self.value = value
        self.city = city
        self.state = state
        self.photo = photo
    }

    //MARK: Types
    struct  PropertyKey {
        static let bonusCode = "bonusCode"
        static let category = "category"
        static let name = "name"
        static let value = "value"
        static let city = "city"
        static let state = "state"
        static let photo = "photo"
    }

    //MARK: NSCoding
    func encode(with aCoder: NSCoder) {
        aCoder.encode(bonusCode, forKey: PropertyKey.bonusCode)
        aCoder.encode(category, forKey: PropertyKey.category)
        aCoder.encode(name, forKey: PropertyKey.name)
        aCoder.encode(value, forKey: PropertyKey.value)
        aCoder.encode(city, forKey: PropertyKey.city)
        aCoder.encode(state, forKey: PropertyKey.state)
        aCoder.encode(photo, forKey: PropertyKey.photo)
    }

    required convenience init?(coder aDecoder: NSCoder) {
        // The name is required. If we cannot decode a name string, the initializer should fail.
        guard let bonusCode = aDecoder.decodeObject(forKey: PropertyKey.bonusCode) as? String else {
            os_log("Unable to decode the Code for a Bonus object.", log: OSLog.default, type: .debug)
            return nil
        }

        // Because photo is an optional property of Meal, just use conditional cast
        let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage

        let category = aDecoder.decodeObject(forKey: PropertyKey.category)
        let value = aDecoder.decodeInteger(forKey: PropertyKey.value)
        let city = aDecoder.decodeObject(forKey: PropertyKey.city)
        let state = aDecoder.decodeObject(forKey: PropertyKey.state)

        // Must call designated initializer.
        self.init(bonusCode: String, category: String, name: String, value: Int, city: String, state: String, photo: UIImage?)
    }
}

错误在 bonusCode: String 上标记,特别是在 String 中的 S 上。

我对编程还很陌生,但我只找到了一个针对这个特定问题的其他搜索结果,而其他类似的似乎与所使用的代码非常具体。

最佳答案

您必须传递解码后的值而不是最后一行中的类型,并且缺少解码名称的行,您必须转换其他字符串对象。强制展开是安全的,因为所有非可选值都已正确编码。

let name = aDecoder.decodeObject(forKey: PropertyKey.name) as! String

let category = aDecoder.decodeObject(forKey: PropertyKey.category) as! String
let value = aDecoder.decodeInteger(forKey: PropertyKey.value)
let city = aDecoder.decodeObject(forKey: PropertyKey.city) as! String
let state = aDecoder.decodeObject(forKey: PropertyKey.state) as! String

...

self.init(bonusCode: bonusCode, category: category, name: name, value: value, city: city, state: state, photo: photo)

关于swift - 无法将类型 'String.Type' 的值转换为预期的参数类型 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49749405/

相关文章:

Swift 命令行工具未接收 DistributedNotificationCenter 通知

ios - Swift 如何使用自定义 UITableView 类将 UITableView 添加到 UIViewController

ios - 如何从 UITabBarViewController 获取对 View Controller 的引用

ios - 带有 completionHandler 的 block 的 Swift 语法...在委托(delegate)方法中

ios - swift post请求编码加号变为空白

ios - NSDecimalNumber 是否有等效的 RoundHalfUp ?

ios - Snapkit,将 UILabel 粘到底部

swift - 更新 cocoapods 会导致文件丢失。

string - swift :insertContentsOf

swift - 使用 Firestore 的自定义模型