NSKeyedUnarchiver - 删除解码数据?

发布于 2024-09-11 00:49:36 字数 499 浏览 5 评论 0原文

我正在使用 NSKeyedArchiver 对一些复杂的对象进行编码并将其保存到磁盘。比如说 -

Class member {
    int *id;
    NSString *name;
    NSMutableArray *array;
    TempClass *object;
}

我试图构建的功能是为了让用户能够保存他的工作,比如说,在创建新成员并稍后返回时。当用户完成后,他点击发布,数据将被传输到网络服务。如果没有,他只需单击“保存”并离开屏幕,数据就会被保留,以便当用户回来时应用程序可以从该点恢复。现在,一旦我将数据发布到网络服务,我就不想再将数据保留在磁盘中,而且我真的找不到删除它的方法。

现在,我的编码和解码类运行良好。我可以使用 NSKeyedArchiver 将数据保存到磁盘并使用 NSKeyedUnarchiver 检索数据。但是,我的问题是,如何删除不再需要的数据?我必须手动删除磁盘上的文件吗?有没有办法让 NSKeyedUnarchiver 删除它返回的数据?

I'm encoding a few complex objects with NSKeyedArchiver and saving it to disk. Say, something like -

Class member {
    int *id;
    NSString *name;
    NSMutableArray *array;
    TempClass *object;
}

The functionality I'm trying to build is for the user to be able to save his work, lets say, while creating a new member and come back to it later. When the user finishes up, he clicks post and the data will be transmitted to a web service. If not, he just clicks save and leaves the screen and the data is persisted, so that the app can resume from that point when the user comes back. Now, once I've posted the data to the web service, I do not want to keep the data in the disk anymore and I can't really find a way to delete it.

Now, my encoding and decoding classes are functioning fine. I can use NSKeyedArchiver to save the data to disk and retrieve it using NSKeyedUnarchiver. But, my question is, how can I delete the data that I don't need anymore? Do I have to manually delete the file on the disk? Is there any way to get NSKeyedUnarchiver to delete the data that's it's returning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分開簡單 2024-09-18 00:49:38

对于斯威夫特 2.0:

func deleteFile(path: String) -> Bool{
    let exists = NSFileManager.defaultManager().fileExistsAtPath(path)
    if exists {
        do {
            try NSFileManager.defaultManager().removeItemAtPath(path)
        }catch let error as NSError {
            print("error: \(error.localizedDescription)")
            return false
        }
    }
    return exists
}

For Swift 2.0:

func deleteFile(path: String) -> Bool{
    let exists = NSFileManager.defaultManager().fileExistsAtPath(path)
    if exists {
        do {
            try NSFileManager.defaultManager().removeItemAtPath(path)
        }catch let error as NSError {
            print("error: \(error.localizedDescription)")
            return false
        }
    }
    return exists
}
别念他 2024-09-18 00:49:38

对于 Swift 3.0 -> 4.1:

let fileManager = FileManager()
let fileName = "your_file_name"

//In Order to get your file path correctly
getFileURL(_ fileName: String) -> String? {
    let fileURL = fileManager.urls(for: fileManager.SearchPathDirectory.documentDirectory, in: fileManager.SearchPathDomainMask.userDomainMask).first
    return (fileURL?.appendingPathComponent(fileName).path)
}


//Persist Data
func persistData(_ data : Data) -> Bool{
     return NSKeyedArchiver.archiveRootObject(data, toFile: getFileURL(fileName)!)
}

//Get Persisted Data
func getArchivedData() -> Data?{
    return NSKeyedUnarchiver.unarchiveObject(withFile: getFileURL(fileName)!) as? Data
}

//Delete Persisted Data 
func deleteArchivedUser() -> Bool{
    do {
        try fileManager.removeItem(atPath: getFileURL(fileName)!)
        return true
    } catch _ {
        return false
    }
}

For Swift 3.0 -> 4.1:

let fileManager = FileManager()
let fileName = "your_file_name"

//In Order to get your file path correctly
getFileURL(_ fileName: String) -> String? {
    let fileURL = fileManager.urls(for: fileManager.SearchPathDirectory.documentDirectory, in: fileManager.SearchPathDomainMask.userDomainMask).first
    return (fileURL?.appendingPathComponent(fileName).path)
}


//Persist Data
func persistData(_ data : Data) -> Bool{
     return NSKeyedArchiver.archiveRootObject(data, toFile: getFileURL(fileName)!)
}

//Get Persisted Data
func getArchivedData() -> Data?{
    return NSKeyedUnarchiver.unarchiveObject(withFile: getFileURL(fileName)!) as? Data
}

//Delete Persisted Data 
func deleteArchivedUser() -> Bool{
    do {
        try fileManager.removeItem(atPath: getFileURL(fileName)!)
        return true
    } catch _ {
        return false
    }
}
烟凡古楼 2024-09-18 00:49:38

对于 Swift 2.0:

do {
  try NSFileManager.defaultManager().removeItemAtPath("Your_PATH")
} catch {

}

For Swift 2.0:

do {
  try NSFileManager.defaultManager().removeItemAtPath("Your_PATH")
} catch {

}
南风起 2024-09-18 00:49:37

发布数据后,可以通过编程方式将其删除的非常简单的方法:

- (BOOL) deleteFile:(NSString *) pathOfFileToDelete error:(NSError *)err {
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: pathOfFileToDelete];
    if(exists) { 
        [[NSFileManager defaultManager]removeItemAtPath: pathOfFileToDelete error:err];
    }
    return exists;
}

A very simple way to just delete it programmatically once you have posted the data:

- (BOOL) deleteFile:(NSString *) pathOfFileToDelete error:(NSError *)err {
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: pathOfFileToDelete];
    if(exists) { 
        [[NSFileManager defaultManager]removeItemAtPath: pathOfFileToDelete error:err];
    }
    return exists;
}
初相遇 2024-09-18 00:49:37

Swift3 示例:

do {
 try FileManager.default.removeItem(atPath: path)
} catch {
 // catch potential error
}

A Swift3 example:

do {
 try FileManager.default.removeItem(atPath: path)
} catch {
 // catch potential error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文