Objective-C:如何让它为 NSCoding 保存两个文件而不是一个?

发布于 2024-12-21 10:44:16 字数 1767 浏览 2 评论 0原文

我的代码在这里:

-(void)loadDataFromDisk
{
    [dict release];
    NSMutableData* data = [NSMutableData dataWithContentsOfFile:[self pathForDataFile]];
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    dict = [[unarchiver decodeObjectForKey:fileKey] retain];
    [unarchiver finishDecoding];
    [unarchiver release];

    if(dict == NULL)
    {
    NSLog(@"First time in.");
    dict = [[NSMutableDictionary alloc] init];
    }
}

-(void)saveDataToDisk
{
    NSMutableData* data = [NSMutableData data];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    [archiver setOutputFormat:NSPropertyListBinaryFormat_v1_0];

    // EncodeObject will automatically encode every primitive inside 'dictionaryToSave', 
    // As for objects, the object class must conform to NSCoding Protocol, else an error will occur.
    [archiver encodeObject:dict forKey:fileKey];
    [archiver finishEncoding];
    [archiver release];

    [data writeToFile:[self pathForDataFile] atomically:YES];    
}

-(NSString*)pathForDataFile
{
     NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString* path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];    
    }
    else
    {
        NSLog(@"error in [PersistentHandler pathForDataFile]");
    }
    // Returns 'directory'/data.bin to method(saveDatatoDisk/loadDataFromDisk)
    return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
}

如果我使用指定的文件密钥(即:“按钮”)保存一个文件一次,它就可以工作。但是如果我想使用指定的文件密钥(即:“时钟”)将另一个文件保存到磁盘,它会用“时钟”覆盖“按钮”,因此“按钮无法再次访问。

如何解决这个困境?我相信这应该是我“saveDataToDisk”(存档)的方式,我应该将一堆文件写入磁盘,而不是用最新的一项替换每一项。

My code is here:

-(void)loadDataFromDisk
{
    [dict release];
    NSMutableData* data = [NSMutableData dataWithContentsOfFile:[self pathForDataFile]];
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    dict = [[unarchiver decodeObjectForKey:fileKey] retain];
    [unarchiver finishDecoding];
    [unarchiver release];

    if(dict == NULL)
    {
    NSLog(@"First time in.");
    dict = [[NSMutableDictionary alloc] init];
    }
}

-(void)saveDataToDisk
{
    NSMutableData* data = [NSMutableData data];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    [archiver setOutputFormat:NSPropertyListBinaryFormat_v1_0];

    // EncodeObject will automatically encode every primitive inside 'dictionaryToSave', 
    // As for objects, the object class must conform to NSCoding Protocol, else an error will occur.
    [archiver encodeObject:dict forKey:fileKey];
    [archiver finishEncoding];
    [archiver release];

    [data writeToFile:[self pathForDataFile] atomically:YES];    
}

-(NSString*)pathForDataFile
{
     NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString* path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];    
    }
    else
    {
        NSLog(@"error in [PersistentHandler pathForDataFile]");
    }
    // Returns 'directory'/data.bin to method(saveDatatoDisk/loadDataFromDisk)
    return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
}

It works if I save a file once with a specified fileKey (i.e: "buttons").. BUT then if I want to save another file to disk with a specified fileKey (i.e: "clocks"), it would overwrite "buttons" with "clocks", so "buttons cannot be accessed again..

How to fix this dilemma? I believe it should be how I "saveDataToDisk" (archive), I should be writing a bunch of files to disk, instead of replacing each one by the most current one.

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

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

发布评论

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

评论(2

明月松间行 2024-12-28 10:44:16

除非我读错了你的代码,否则你总是使用相同的路径来保存文件。这就是它被覆盖的原因。

return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];

每次保存文件时,文件位于 /path/you/retrieve/data.bin

您需要更改文件名,或者保存文件的目录。如果您想保持相同的文件名,则每次都创建一个新目录您可以保存并使用时间戳或所保存内容的描述来标记目录。

Unless I am reading your code wrong, you are always using the same path to save the file to. That is why it is overwriting.

return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];

Every time you save the file is at /path/you/retrieve/data.bin

You need to change the filename, or, the directory that it is saved in. If you want to keep the same filename then make a new directory every time you save and just label the directory with a time stamp or a description of what you are saving.

素染倾城色 2024-12-28 10:44:16

您总是写入“~/Documents/data.bin”——您的 pathForDataFile 方法始终返回该路径。如果要保存到不同的文件,则应该写入不同的文件。

You're always writing to "~/Documents/data.bin" — your pathForDataFile method always returns that path. You should write to different files if you want to save to different files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文