NSKeyedArchiver没有持久化数据Swift 3

时间:2016-12-15 13:57:10

标签: ios swift swift3 nskeyedarchiver

我有一个正在制作的应用程序我正在尝试从Swift 2.2转换为Swift 3.我在XCode 8.1和XCode 8.2中都尝试过Swift 3代码。

以下Swift 2代码完美运行:

func saveItemsToCache() {
    NSKeyedArchiver.archiveRootObject(items, toFile: itemsCachePath)
}

func loadItemsFromCache() {
    if let cachedItems = NSKeyedUnarchiver.unarchiveObjectWithFile(itemsCachePath) as? [TruckItem] {
        items = cachedItems
    }
}

var itemsCachePath: String {
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let fileURL = documentsURL.URLByAppendingPathComponent("Trucks.dat")
    return fileURL.path!
}

但是当我使用为Swift 3转换的相同代码时,数据没有被持久化:

func saveItemsToCache() {
    print("SAVED TRUCKS:", items)
    NSKeyedArchiver.archiveRootObject(items, toFile: itemsCachePath)
}

func loadItemsFromCache() {
    if let cachedItems = NSKeyedUnarchiver.unarchiveObject(withFile: itemsCachePath) as? [TruckItem] {
        items = cachedItems
        print("LOADED TRUCKS:", items)
    }
}

var itemsCachePath: String {
    let documentsURL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    let fileURL = documentsURL.appendingPathComponent("Trucks.dat")
    return fileURL.path
}

控制台输出示例:

SAVED TRUCKS: [<TruckTelematics.TruckItem: 0xc852380>, <TruckTelematics.TruckItem: 0x9b23ba0>]

LOADED TRUCKS: []

1 个答案:

答案 0 :(得分:0)

我最近发现问题根本不在于NSKeyedArchiver,而是在我的NSObject子类convenience init?(coder aDecoder: NSCoder)中使用TruckItem方法。

在Swift 2.2中,您将解码不同的对象属性,如下所示:

let IMEI = aDecoder.decodeObject(forKey: CodingKeys.IMEI) as! String
let active = aDecoder.decodeObject(forKey: CodingKeys.active) as! Bool
let daysInactive = aDecoder.decodeObject(forKey: CodingKeys.daysInactive) as! Int

在Swift 3中,不是对所有属性类型使用decodeObject(),而是显示现在有一些新功能需要注意。以下是在Swift 3中解码的相同对象属性:

let IMEI = aDecoder.decodeObject(forKey: CodingKeys.IMEI) as! String
let active = aDecoder.decodeBool(forKey: CodingKeys.active)
let daysInactive = aDecoder.decodeInteger(forKey: CodingKeys.daysInactive)

我花了很长时间才发现这一点,希望这个答案可以让其他用户免于类似的挫折。

相关问题