NSKeyedArchiver写XML(或其他人类可读)? [英] NSKeyedArchiver write XML (or other human readable)?

查看:112
本文介绍了NSKeyedArchiver写XML(或其他人类可读)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让NSKeyedArchiver以人类可读的形式写出我的数据(即不是一个二进制文件)。我理解我可以使用...

I have been trying to get the NSKeyedArchiver to write out my data in a human readable form (i.e. not as a binary file) I understand that I can use ...

setOutputFormat: NSPropertyListXMLFormat_v1_0

似乎无法获得正确的语法,任何人都可以指向正确的方向?

But ... I just can't seem to get the syntax right, can anyone point me in the right direction?

NSData *artistData;

NSLog(@"(*) - Save All");
artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection ];
[artistData writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];



EDIT



我添加了setOutputFormat

EDIT

I added setOutputFormat (see below) but I get a warning at compile, what am I missing?

warning: NSData  may not respond to 'setOutputFormat:'

这是我使用的代码。

NSLog(@"(*) - Save All");
artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection ];
[artistData setOutputFormat: NSPropertyListXMLFormat_v1_0];
[artistData writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];

gary

推荐答案

这是你必须做的:

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.

这篇关于NSKeyedArchiver写XML(或其他人类可读)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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