且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSKeyedArchiver写XML(或其他人类可读)?

更新时间:2022-12-18 11:26:03

这是你必须做的:

This is what you have to do:

NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archiver encodeObject:artistCollection forKey:@"root"];
[archiver finishEncoding];
[data writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];
[archiver release];

请注意,您必须使用 [archiver encodeObject :artistCollection forKey:@root] 而不是 [archiver encodeRootObject:artistCollection]; 这是因为NSKeyedArchiver不会覆盖NSCoder encodeRootObject:方法。我认为这是一个错误,并会报告给苹果。

Note that you must use [archiver encodeObject:artistCollection forKey:@"root"] instead of [archiver encodeRootObject:artistCollection]; as suggested by Sean. This is because NSKeyedArchiver does not override NSCoder encodeRootObject: method. I consider this a bug and will report it to Apple.

现在,真正的问题是,为什么要写XML而不是默认的二进制格式?如果这是为了调试目的,我建议您使用 TextWrangler ,这将允许您透明地编辑二进制plist文件,就像它们是XML一样。

Now, the real question is why do you want to write XML instead of the default binary format? If it's for debugging purpose, I suggest you to use TextWrangler which will allow you to transparently edit binary plist files as if they were XML.