gpt4 book ai didi

ios - 在 NSUserDefaults 中保存 Realm 对象子类?

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:25 24 4
gpt4 key购买 nike

我正在将 Realm 引入到我的 swift 项目中。我有一个 User 类,我将其实例保存到 NSUserDefaults 中以跟踪第一个登录用户。

在使 User 成为 Object 的子类后,我在尝试取消存档时遇到以下错误(存档似乎工作正常):

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (RLMStandalone_User) for key (root); the class may be defined in source code or a library that is not linked'

我将 Realm 安装为 Cocoapod,这是 User 类中的相关方法

static var currentUser: User? {
get {
if let data = NSUserDefaults.standardUserDefaults().objectForKey(UserDefaultKeys.kUserData) as? NSData,
let user = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? User {
return user
} else {
return nil
}
}
set {
if let user = newValue {
let data = NSKeyedArchiver.archivedDataWithRootObject(user)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: UserDefaultKeys.kUserData)
NSUserDefaults.standardUserDefaults().synchronize()
} else {
NSUserDefaults.standardUserDefaults().removeObjectForKey(UserDefaultKeys.kUserData)
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}

// MARK: NSCoding

convenience init?(coder decoder: NSCoder) {
self.init()
guard let firstName = decoder.decodeObjectForKey("firstName") as? String,
let lastName = decoder.decodeObjectForKey("lastName") as? String,
let email = decoder.decodeObjectForKey("email") as? String,
let icloud = decoder.decodeObjectForKey("icloudUserID") as? String,
let userType = decoder.decodeObjectForKey("userType") as? String
else {
return nil
}

self.firstName = firstName
self.lastName = lastName
self.email = email
self.profilePic = decoder.decodeObjectForKey("profilePic") as? String
self.icloudUserID = icloud
self.userType = userType
self.coverPhoto = decoder.decodeObjectForKey("coverPhoto") as? String
self.facebookID = decoder.decodeObjectForKey("facebookID") as? String
self.placeID = decoder.decodeObjectForKey("placeID") as? String
}

func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.firstName, forKey: "firstName")
coder.encodeObject(self.lastName, forKey: "lastName")

coder.encodeObject(self.icloudUserID, forKey: "icloudUserID")
coder.encodeObject(self.userType, forKey: "userType")
coder.encodeObject(email, forKey: "email")

if let coverPhotoUrl = self.coverPhotoUrl {
coder.encodeObject(coverPhotoUrl, forKey: "coverPhoto")
}

if let profilePicUrl = self.profilePicUrl {
coder.encodeObject(profilePicUrl, forKey: "profilePic")
}

if let fbID = self.facebookID {
coder.encodeObject(fbID, forKey: "facebookID")
}

if let placeID = self.placeID {
coder.encodeObject(placeID, forKey: "placeID")
}


}

最佳答案

不可能在 NSUserDefaults 中存储 Realm Object,因为(正如您在该错误消息中看到的那样)它们不能被 NSCoding< 序列化或反序列化 协议(protocol)。

相反,最好将主键属性添加到您的 User 对象(这样您就可以使用它从 Realm 中查询该对象),并将主键本身存储在 NSUserDefaults 代替。

或者更好的是,与其依赖 NSUSerDefaults,不如简单地在您的模型中拥有一个 bool 属性 isCurrent,并使用它来计算出哪个更好用户是当前用户。

关于ios - 在 NSUserDefaults 中保存 Realm 对象子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com