簡體   English   中英

iOS(Swift):核心數據可轉換屬性

[英]iOS (Swift): Core data transformable attributes

我有一個采用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
    }

}

我想成為Watch實體的Transformable屬性:

final class Watch: NSManagedObject {
    @NSManaged public fileprivate(set) var time: Time
}

如下所示:

在此處輸入圖片說明

我已成功將其保存到托管對象上下文中,但是當我重新加載應用程序時, time屬性為nil

我在這里想念什么嗎? 為什么此屬性無法成功保存? 這似乎是其他職位所需要的。

非常感謝您的幫助!

@IraniyaNaynesh在問題注釋中的建議是紅色鯡魚。

答案很簡單。 更改decodeObjectdecodeIntegerinit?(coder aDecoder: NSCoder)方法和數據恢復BLOBS ,這不是 nil和已成功保存,從SQLite數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM