- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在寻找这个问题的答案,但我找不到任何答案,类似的问题根本没有答案。基本上,我想使用 NSKeyedUnarchiver.archiveRootObject() 保存数据并使用 .unarchiveObject(withFile) 加载数据。它在 Swift 2.3 上工作得很好,现在它崩溃了,说未归档部分总是返回 nil。我还检查该文件是否存在并且确实存在。我真的不知道发生了什么事。这是加载过程:
func loadnotifs(_ username:String)->[AFNotificationData]{
let ArchiveURL = Constants.DocumentsDirectory.appendingPathComponent(username)
print("loading " + ArchiveURL.path)
if FileManager.default.fileExists(atPath: ArchiveURL.path) {
let unarchived = NSKeyedUnarchiver.unarchiveObject(withFile: ArchiveURL.path) as? [AFNotificationData]
if unarchived != nil {
return NSKeyedUnarchiver.unarchiveObject(withFile: ArchiveURL.path) as! [AFNotificationData]
} else {
return []
}
}
else {
return []
}
}
这就是节省的费用:
func savenotifs(_ username:String){
if username != "" {
let ArchiveURL = Constants.DocumentsDirectory.appendingPathComponent(username)
print("saving " + ArchiveURL.path)
}
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(AFDatabase.sharedDatabase.notificationList, toFile: ArchiveURL.path)
if !isSuccessfulSave {
print("Failed to save notifs")
}
}
}
但最后我总是得到“ fatal error :在解包可选值时意外发现 nil”
最佳答案
我找错地方了。其实保存和加载过程都很好,问题是需要遵守 NSCoding 协议(protocol)所需的 init:我正在使用
aDecoder.decodeObject(forKey:xxx) as! Bool
而不是新的
aDecoder.decodeBool(forKey:xxx)
事实上,它是由 swift 2.3->3 转换器使用错误的命令自动更改的,请注意这一点!
此外,请注意,因为此命令与 swift 2.3 中的 bool 值/整数不兼容。
关于swift3 - NSKeyedArchiver.unarchiveObject Swift 3 iOS 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39830142/
我想将用户的过滤器选择保存在 FilterViewController 上。 当 FilterViewController 关闭时,NSKeyedArchiver.archiveRootObject
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
这个问题在这里已经有了答案: UIImage(contentsOfFile:) returning nil despite file existing in caches directory [du
我有一个有效但已弃用的工作代码: 这部分没问题: let archived = try? NSKeyedArchiver.archivedData(withRootObject: [defaultR
在 iOS 12.1 中,unarchiveObject(withFile:) 已被弃用。 如何将 NSKeyedUnarchiver.unarchiveObject(withFile: String
我一直在寻找这个问题的答案,但我找不到任何答案,类似的问题根本没有答案。基本上,我想使用 NSKeyedUnarchiver.archiveRootObject() 保存数据并使用 .unarchiv
我尝试将代码从 NSCoding 格式化为 Codable,旧版本使用 NSKeyedUnarchiver 取消存档。 // ItemStore.swift var allItems = [Item]
我正在尝试在 Today 小部件和主机应用程序之间传递自定义类。我正在为此使用 NSKeyArchiver/NSKeyUnarchiver 类。我的类(class)符合 NSCoding 协议(pro
我是一名优秀的程序员,十分优秀!