[英]iOS (Swift): Core data transformable attributes
I have a simple Time
class that adopts NSCoding
protocol: 我有一个采用
NSCoding
协议的简单Time
类:
class Time: NSObject, NSCoding {
var hours: Int
func encode(with aCoder: NSCoder) {
aCoder.encode(self.hours, forKey: "hours")
}
public required init?(coder aDecoder: NSCoder) {
guard let hours = aDecoder.decodeObject(forKey: "hours") as? Int
else { return nil }
self.hours = hours
}
init(hours: Int) {
self.hours = hours
}
}
which I want to be a Transformable
attribute to my Watch
entity: 我想成为
Watch
实体的Transformable
属性:
final class Watch: NSManagedObject {
@NSManaged public fileprivate(set) var time: Time
}
as shown here: 如下所示:
I successfully save this to the managed object context but when I reload the app the time
attribute is nil
. 我已成功将其保存到托管对象上下文中,但是当我重新加载应用程序时,
time
属性为nil
。
Am I missing something here? 我在这里想念什么吗? Why doesn't this property successfully save?
为什么此属性无法成功保存? This seems to be all that is required in other posts.
这似乎是其他职位所需要的。
Thanks a lot for any help! 非常感谢您的帮助!
The suggestion in the comments of the question by @IraniyaNaynesh is a red herring. @IraniyaNaynesh在问题注释中的建议是红色鲱鱼。
The answer turns out to be quite simple. 答案很简单。 Change
decodeObject
to decodeInteger
in the init?(coder aDecoder: NSCoder)
method and the data restores the BLOBS
, which are not nil
and have been saved successfully, from the SQLite database. 更改
decodeObject
到decodeInteger
中init?(coder aDecoder: NSCoder)
方法和数据恢复BLOBS
,这不是 nil
和已成功保存,从SQLite数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.