如何使用 NSKeyedArchiver 以 NSPropertyListXMLFormat_v1_0 格式将对象序列化为文件?

发布于 2024-07-16 07:59:36 字数 284 浏览 5 评论 0原文

希望这个标题足够清楚。 无论如何,似乎:

[NSKeyedArchiver archiveRootObject:rootObject toFile:path];

仅限于 NSPropertyListBinaryFormat_v1_0

我需要人类可读的格式[so xml]。 如果您感兴趣,这是我最终将在 github 上发布的项目的一部分,其中包含一篇关于“如何对 GPS 应用程序进行单元测试”的博客文章

:-)

Hopefully that title is clear enough. Anyways, it seems:

[NSKeyedArchiver archiveRootObject:rootObject toFile:path];

is restricted to NSPropertyListBinaryFormat_v1_0.

I need the format to be human readable [so xml]. If you're interested, this is part of project I'll eventually put up on github with a blog post about "How to Unit Test GPS Applications"

:-)

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

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

发布评论

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

评论(3

我不吻晚风 2024-07-23 07:59:36

您必须使用 NSPropertyListSerialization 来转换它。 基本上,假设您要序列化的对象名为 myObject 并且最终文件存储在 myPath 中,您可以使用类似以下代码片段的内容将其序列化为 XML:

NSString *error = nil;
NSData *mySerializedObject = [NSKeyedArchiver archivedDataWithRootObject:myObject];
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:mySerializedObject
                        format:NSPropertyListXMLFormat_v1_0
                        errorDescription:&error]:
if( xmlData ) {
    [xmlData writeToFile:myPath atomically:YES];
} else {
    NSLog(error);
    [error release];
}

You have to use NSPropertyListSerialization to convert it. Basically, assuming the object that you're serializing is called myObject and the ultimate file is stored in myPath, you would serialize it to XML using something like the following code fragment:

NSString *error = nil;
NSData *mySerializedObject = [NSKeyedArchiver archivedDataWithRootObject:myObject];
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:mySerializedObject
                        format:NSPropertyListXMLFormat_v1_0
                        errorDescription:&error]:
if( xmlData ) {
    [xmlData writeToFile:myPath atomically:YES];
} else {
    NSLog(error);
    [error release];
}
棒棒糖 2024-07-23 07:59:36

不要使用类方法,而是尝试创建 NSKeyedArchiver 的实例并使用 setOutputFormat:NSPropertyListXMLFormat_v1_0 指定 XML 格式。 唯一的缺点是它需要几行额外的代码,因为要实际处理序列化,它不会只一步返回一个 NSData 对象。

Instead of using the class method, try creating an instance of NSKeyedArchiver and using setOutputFormat:NSPropertyListXMLFormat_v1_0 to specify XML format. The only downside is it'll take a few extra lines of code since to actually handle the serialization, it won't just return an NSData object in one step.

提笔书几行 2024-07-23 07:59:36

您必须使用方法对对象类进行编码
-(void)encodeWithCoder(NSCoder *)编码器{

[coder encodeObject:name forKey:@"name"];
[coder encodeObject:address forKey:@"address"];
[coder encodeObject:position forKey:@"position"];

}

You will have to encode the object class by using method
-(void) encodeWithCoder(NSCoder *)encoder {

[coder encodeObject:name forKey:@"name"];
[coder encodeObject:address forKey:@"address"];
[coder encodeObject:position forKey:@"position"];

}

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