Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将模型保存到Userdefaults会使应用程序swift崩溃_Swift_Codable_Userdefaults - Fatal编程技术网

将模型保存到Userdefaults会使应用程序swift崩溃

将模型保存到Userdefaults会使应用程序swift崩溃,swift,codable,userdefaults,Swift,Codable,Userdefaults,我试图用UserDefault保存我的模型类,但不断得到错误 -[\uu SwiftValue ENCODER:]:无法识别的选择器发送到实例0x2825fc0a0' 我正在使用codables,下面是我的模型的样子 struct AuthModel: Codable { let token: String? let firstName: String? let lastName: String? let telephone: String? let us

我试图用UserDefault保存我的模型类,但不断得到错误
-[\uu SwiftValue ENCODER:]:无法识别的选择器发送到实例0x2825fc0a0'

我正在使用codables,下面是我的模型的样子

struct AuthModel: Codable {
    let token: String?
    let firstName: String?
    let lastName: String?
    let telephone: String?
    let userId: Int?
    let email: String?
    let isEmailConfirmed: Int?
    let isVerified: Int?

    enum AuthModelCodingKeys: String, CodingKey {
        case token
        case firstName = "first_name"
        case lastName = "last_name"
        case telephone
        case userId = "user_id"
        case email
        case isEmailConfirmed = "is_email_confirmed"
        case isVerified = "is_verified"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: AuthModelCodingKeys.self)
        token = try container.decodeIfPresent(String.self, forKey: .token)
        firstName = try container.decodeIfPresent(String.self, forKey: .firstName)
        lastName = try container.decodeIfPresent(String.self, forKey: .lastName)
        telephone = try container.decodeIfPresent(String.self, forKey: .telephone)
        userId = try container.decodeIfPresent(Int.self, forKey: .userId)
        email = try container.decodeIfPresent(String.self, forKey: .email)
        isEmailConfirmed = try container.decodeIfPresent(Int.self, forKey: .isEmailConfirmed)
        isVerified = try container.decodeIfPresent(Int.self, forKey: .isVerified)
    }
}
上面的模型显示了我试图在UserDefault中保存的内容,但每当它试图保存时,我总是会遇到崩溃

我使用我的用户默认设置如下

public func saveCurrentUser(_ user: AuthModel) {
        put(user, forKey: userKey)
    }

    private func put(_ value: Any?, forKey key: String) {
        guard let value = value else {
            storage.removeObject(forKey: key)
            return
        }
        storage.setValue(NSKeyedArchiver.archivedData(withRootObject: value), forKey: key)
    }

使用
jsonecoder()。然后使用相关的
键将此
数据
保存到
UserDefaults

public func saveCurrentUser(_ user: AuthModel) {
    do {
        let data = try JSONEncoder().encode(user)
        UserDefaults.standard.set(data, forKey: userKey)
    } catch  {
        print(error)
    }
}

Codable
NSCoding
是两个完全不相关的独立概念。我使用的是
Codable
,这就是我的观点<代码>编码
与使用
NSKeyedArchiver
毫无关系。您正在尝试混合使用两种不相关的序列化方法。如何解决此问题我还存储了一些字符串值您所说的字符串值是什么?如果答案对您有帮助,请接受并向上投票。快乐编码..是的,它做到了。Cheers@PGDevI正在尝试获取已保存的用户,我得到了一个零返回任何帮助,请