删除使用NSCoding存档的文件 [英] Deleting files archived with NSCoding

查看:47
本文介绍了删除使用NSCoding存档的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSCoding删除我的应用程序已持久保存的所有文件.看起来这应该是一件简单的事情,但是我找不到答案,虽然我在这里只显示一个类,但我有几个,并且我希望使用delete函数来保存我的应用程序中所有类的所有实例已存档.我没有做与NSCoder不同的事情:

I'm trying to delete all files that my application has persisted using NSCoding. Seems like it should be a simple thing to do, but I can't find an answer, whilst I'm showing just one class here, I have several and I'd like the delete function to save all instances of all classes my app has archived. I'm not doing anything different to NSCoder:

因此定义了我的界面

Details : NSObject <NSCoding>

我的实现有:

- (id)initWithCoder:(NSCoder *)decoder
{
    NSLog(@"initWithCoder 'Details'");

    if (self = [super init])
    {
        NSLog(@"Decoding 'Details'");

        self.name = [decoder decodeObjectForKey:@"name"];
    }

    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    NSLog(@"Encoding 'Details'");
    [encoder encodeObject:self.name forKey:@"name"];
}

当我写它的时候我正在使用:

When I write it I'm using:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:data forKey:identifier];

我确实开始尝试查看documents目录,但是我在写这篇文章时认为,根据我的研究,它将被写入NSUserDefaults(这可能不是最好的方法).因此冒着问两个问题的风险,我该如何删除使用NSCoder保存/归档的所有对象到NSUserDefaults(以及是否有可能,我应该将其设置为其他内容(例如,documents文件夹内的子文件夹....如果是,我该怎么做.

I did start off trying to look in the documents directory, but I think as I'm writing this and from my research that it will be written to NSUserDefaults (and that might not be the best thing). So at the risk of asking two questions, how do I delete all my objects that are saved/archived using NSCoder to NSUserDefaults (and potentially, should I be setting this to something else (i.e a sub-folder inside the documents folder....if yes, how would I do that).

推荐答案

NSCoding 提供了一种将对象转换为 NSData 以便于归档的方法.之后,您要对那些 NSData 对象进行处理.您可能会或可能不会将它们保存到单个文件中,但是 NSCoding 对此并不了解.

NSCoding provides a way to transform objects into NSData for easy archiving. What you do with those NSData objects afterward are your responsibility. You may or may not save them to individual files, but NSCoding is not aware of that.

似乎您已经使用 [userDefaults setObject:data forKey:identifier]; 将这些 NSData 转储到 NSUserDefaults 中.您需要通过执行 [userDefaults removeObjectForKey:identifier]; 来删除已使用的每个 identifier 的所有条目.您无法自动确定这些条目是什么.

It seems like you have been dumping those NSData into NSUserDefaults using [userDefaults setObject:data forKey:identifier]; You need to delete all entries for every identifier you have used by doing [userDefaults removeObjectForKey:identifier];. You can't automatically determine what those entries are.

此外,您可能希望考虑不要占用 NSUserDefaults 并将其保存到单个文件中.(请参见 [NSData writeToFile:] ).这将另外帮助您跟踪这些文件是什么(例如,您可以将所有文件都放在同一文件夹/子文件夹中).

Additionally, you might want to consider not hogging NSUserDefaults and save those to individual files. (look at [NSData writeToFile:]) This will additionally help you track what those files are (you could for example all place them in the same folder/subfolder).

这篇关于删除使用NSCoding存档的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆