我正在尝试使用NSKeyedArchiver快速保存一个非常简单的对象,并且看到它正确保存了带有值的对象,但是每当它尝试对保存的数据进行解码时,它都会失败。我是新手,我已经尝试使用Google搜寻和os_log(ing)进行所有操作,但是我看不出问题出在哪里。这是我要保存的对象...
class TrackedAmount: NSObject, NSCoding {
var cups: Int
var fraction: Float?
struct PropertyKey {
static let cups = "cups"
static let fraction = "fraction"
}
init?(cups: Int, fraction: Float?) {
os_log("Running init? function", log: .default, type: .debug)
if(cups < 0) {
os_log("Cups less than 0. Returning nil", log: .default, type: .debug)
return nil
}
self.cups = cups
self.fraction = fraction
}
required convenience init? coder aDecoder: NSCoder) {
os_log("Running required convenience init? function", log: .default, type: .debug)
guard let cups = aDecoder.decodeObject(forKey: PropertyKey.cups) as? Int else {
os_log("Unable to decode cups", log: .default, type: .debug)
return nil
}
guard let fraction = aDecoder.decodeObject(forKey: PropertyKey.fraction) as? Float else {
os_log("Unable to decode fraction", log: .default, type: .debug)
return nil
}
self.init(cups: cups, fraction: fraction)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(cups, forKey: PropertyKey.cups)
aCoder.encode(fraction, forKey: PropertyKey.fraction)
}
然后在ViewController中,我试图像这样获取并保存它们,...
private func saveAmount() {
trackedToday = TrackedAmount(cups: selectedCups, fraction: selectedFraction)
print("data.... cups: " + String(trackedToday!.cups) + " fractions: " + String(trackedToday!.fraction!))
let fullPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("dailytracked")
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: trackedToday!, requiringSecureCoding: false)
try data.write(to: fullPath)
} catch {
os_log("Unable to save tracking amount", log: .default, type: .debug)
}
}
private func loadDailyTracked() -> TrackedAmount? {
let fullPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("dailytracked")
if let nsData = NSData(contentsOf: fullPath) {
do {
let data = Data(referencing: nsData)
if let loadedData = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? TrackedAmount {
os_log("it finally worked", log: .default, type: .debug)
return loadedData
}
} catch {
os_log("Couldn't read from file", log: .default, type: .debug)
return nil
}
return nil
}
它可以正确保存,但是每当我尝试运行loadDailyTracked()方法时,我都会在xcode的输出部分得到以下内容...
运行所需的便利init?功能
无法解码杯子
我似乎无法弄清为什么它不能解码杯子,我知道数据已保存,因为除了试图从保存的数据中读取数据外,没有任何日志显示任何故障。在保存之前,我什至记录了信息,并且显示了正确的数据。有人有想法么?我真的是IOS和Swift的新手,我只是想不通。谢谢。
*请注意,如果您需要更多代码或信息,我很乐意提供,我只是想尽量避免使该帖子过大。
答案 0 :(得分:2)
就NSCoder
和Int
之类的Float
标量类型而言,不是对象,并且专用方法不会返回可选值
required convenience init? coder aDecoder: NSCoder) {
os_log("Running required convenience init? function", log: .default, type: .debug)
let cups = aDecoder.decodeInteger(forKey: PropertyKey.cups)
let fraction = aDecoder.decodeFloat(forKey: PropertyKey.fraction)
self.init(cups: cups, fraction: fraction)
}
Swift中的 NSCoding
很重。强烈建议使用Codable
协议来序列化结构和类。