首页 > 解决方案 > 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 找到本地存储的图像,但找不到编码数据。

标签: swiftxcodesimulatorpersistentnscoder

解决方案


推荐阅读