是否可以使用对象归档来存储多个对象?
好吧,当然有可能,这没问题。在我的代码中,我有多个使用的对象,它们需要是数据持久性的。为此,我使用了对象编码并将它们存储在数据文件中。例如:
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
然后在另一段代码中进一步进行:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSLog(@"Test 4");
[archiver encodeObject:playerTwo forKey:@"PlayerTwoObject"];
[archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
[archiver encodeObject:playerOne forKey:@"PlayerOneObject"];
[archiver encodeObject:settings forKey:@"Settings"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[singlePlayer release];
[playerOne release];
[playerTwo release];
[settings release];
然后保存对象。我知道它有效,因为我可以使用以下代码将它们删除:
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]];
SettingsObject *settings = [unarchiver decodeObjectForKey:@"Settings"];
Player *test = [unarchiver decodeObjectForKey:@"PlayerOneObject"];
NSLog(@"Power On Settings Test: %d,%d", [settings playerOneStartingLife], [settings singlePlayerStartingLife]);
NSLog(@"Power On Player Test: %@,%d", [test name], [test life]);
酷!所以现在我可以存储我的对象并读出它们。 这就是问题所在。在另一个类中,我取出我正在存储的对象之一并使用它。不是全部,我只读了一篇。然后我更改设置后再次保存它。但我只保存它:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[singlePlayer release];
完成此操作后,我存储的所有其余对象都将被删除。现在我很困惑,因为我以为我只是编辑一键“SinglePlayer”,但显然这以某种方式删除了其余的?这应该发生吗?我在发布存档器时遇到了一些问题,这是否会影响它......我认为不会。有没有人对此有一些见解,因为如果我只想编辑其中一个对象,我不想必须取出所有对象并重新编码它们...
感谢您的帮助!
Okay, of course it is possible, that's no issue. In my code I have multiple objects that I work with, and they need to be data persistent. To do that I've used object encoding and storing them in a data file. Such as:
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
And then further along in another section of code:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSLog(@"Test 4");
[archiver encodeObject:playerTwo forKey:@"PlayerTwoObject"];
[archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
[archiver encodeObject:playerOne forKey:@"PlayerOneObject"];
[archiver encodeObject:settings forKey:@"Settings"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[singlePlayer release];
[playerOne release];
[playerTwo release];
[settings release];
And that saves the objects. I know it works, because I can take them out with this code:
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]];
SettingsObject *settings = [unarchiver decodeObjectForKey:@"Settings"];
Player *test = [unarchiver decodeObjectForKey:@"PlayerOneObject"];
NSLog(@"Power On Settings Test: %d,%d", [settings playerOneStartingLife], [settings singlePlayerStartingLife]);
NSLog(@"Power On Player Test: %@,%d", [test name], [test life]);
Cool! So now I can store my objects, and read them out. So this is where the issue comes in. In a different class I take out one of the objects I'm storing and work with it. Not all of them, I only read out one. And then I save it again after I change the settings. But I only save it:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[singlePlayer release];
After I have done that, all the rest of the objects I have stored are removed. Now I'm confused, because I thought I was only editing the one key "SinglePlayer", but apparently that somehow removes the rest of them? Is that supossed to happen? I have had some issues with releasing the archiver, would that affect it at all... I don't think it would. Does anyone have some insight on this, because I don't want to have to take all of the objects out and re-encode them if I only want to edit one of them...
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用空数据对象创建一个新的归档器。您将 SinglePlayer 对象写入数据中,但不将其余数据存储在同一个新创建的存档中。
为此,请取消存档之前存储的整个存档,然后调整 SinglePlayer 对象并存档并再次保存。
You create a new archiver, with an emtpy data object. You write the SinglePlayer object in the data, but you do not store the rest of the data in the same, newly created archive.
To do so, unarchive the whole previously stored archive, then adjust the SinglePlayer object and archive and save it again.