swift - Xcode - 从模拟器访问持久数据 (NSCoder)
问题描述
我正在使用 NSCoder 在我的应用程序中存储持久数据,如下所示:
class UserNotification: NSObject, NSCoding {
var message: String!
init(message: String) {
self.message = message
}
required convenience init(coder aDecoder: NSCoder) {
let message = aDecoder.decodeObject(forKey: "message") as! String
self.init(message: message)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(message, forKey: "message")
}
}
这是它们被加载到应用程序中的方式:
let defaults = UserDefaults.standard
if (defaults.object(forKey: "userNotifications") != nil) {
let decoded = defaults.object(forKey: "userNotifications") as! Data
let decodedUserNotifications = NSKeyedUnarchiver.unarchiveObject(with: decoded as Data) as! [UserNotification]
userNotifications = decodedUserNotifications
}
我还为用户提供了一种清除所有保存数据的方法。为了清算,我实施了:
let newListData = NSKeyedArchiver.archivedData(withRootObject: userNotifications)
let defaults = UserDefaults.standard
defaults.set(newListData, forKey: "userNotifications")
defaults.synchronize()
所有用户通知对象都存储在一个全局可访问的数组中:
userNotifications = [UserNotification]()
我想确保在用户清除所有持久数据后删除它们。我正在查看我的模拟器的文件夹(~/Library/Developer/CoreSimulator/Devices)以找到持久数据并查看它是否被删除,但我没有设法在任何地方找到它。
运行应用程序时似乎一切正常(保存数据、擦除数据等),但我想确保设备上的所有数据都被正确擦除。有一些缩略图被保存,因此我想确保它真的被删除并且不占用不必要的空间。
所以我的问题是使用 NSCoder 时数据存储在哪里?我可以使用 FileManager 找到本地存储的图像,但找不到编码数据。
解决方案
推荐阅读
- javascript - 在页面加载时触发动画
- swift - 如何在 NSPopover Swift 中使用“在字典中查找”
- typescript - AWS AppMesh Typescript CDK:合适的使用范围是什么?
- pytorch - 通过将数据集分成块并再次加载模型,在整个数据集上训练模型,直到训练完数据集的所有块
- sql - 如何获取在同一个表中定义为单独行的子项目类别
- math - 为什么游戏引擎中的坐标是反向的?
- php - 我需要函数similar_text的类似物,但考虑到子字符串
- javascript - 游戏结束警报后如何重置我的游戏?
- google-analytics - 谷歌分析收购报告差异
- pdf - MS Edge (Chromium) 不显示设置了内容安全策略 (ERR_BLOCKED_BY_CLIENT) 的 PDF 文档