Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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
Ios NSKeyedArchiver未持久化数据Swift 3_Ios_Swift_Swift3_Nskeyedarchiver - Fatal编程技术网

Ios NSKeyedArchiver未持久化数据Swift 3

Ios NSKeyedArchiver未持久化数据Swift 3,ios,swift,swift3,nskeyedarchiver,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 = NSKeye

我有一个正在生产的应用程序,我正试图从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: []
保存的卡车:[,]
装载的卡车:[]

我最近发现问题根本不在于NSKeyedArchiver,而在于我的NSObject子类
TruckItem
中的
便利init?(coder-aDecoder:NSCoder)
方法

在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)

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

我最近发现问题根本不在于NSKeyedArchiver,而在于我的NSObject子类
TruckItem
中的
便利init?(coder-aDecoder:NSCoder)
方法

在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)
我花了相当长的时间才发现这一点,希望这个答案能让其他用户免于类似的挫折