我试图在swift中使用NSKeyedArchiver保存一个非常简单的对象,我看到它正在使用值正确保存,但每当它尝试解码保存的数据时它就会失败。我是swift的新手,我尝试过googling和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的新手,我无法弄明白这一点。谢谢。
*旁注,如果您需要更多代码或信息,我很乐意提供它,我只是试图保持帖子不要太大,如果它不需要。
在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)
}
斯威夫特的NSCoding
很重。强烈建议使用Codable
协议来序列化结构和类。