使用Obj-C/iPhone SDK以XML格式进行对象序列化 [英] Object serialization in XML format using Obj-C / iPhone SDK

查看:95
本文介绍了使用Obj-C/iPhone SDK以XML格式进行对象序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用相对较少数据量的iPhone应用程序. 我想将数据保存为XML格式,并能够将其在内存中加载到Objective-C对象.我想使用iPhone SDK工具,例如 NSPropertyListSerialization writeToFile:atomically:类和方法.

I have an iPhone App that uses a relatively small amount of data. I'd like to save the data in the XML format and be able to load this in memory to Objective-C objects. I'd like to use iPhone SDK facilities like NSPropertyListSerialization or writeToFile:atomically: classes and methods.

NSPropertyListSerialization文档

NSPropertyListSerialization类 提供转换属性的方法 列出与多个对象之间的对象 序列化格式.物业清单 对象包括NSData,NSString, NSArray,NSDictionary,NSDate和 NSNumber对象.

The NSPropertyListSerialization class provides methods that convert property list objects to and from several serialized formats. Property list objects include NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber objects.

假设我想保存几个属性均为属性列表类型的 Person 对象(一个族).我设法以XML格式保存此文件的唯一方法是:

Suppose I'd like to save a couple of Person objects (a family) whose attributes are all property-list types. The only-way I managed to save this in XML Format is by doing:

// container for person objects
NSMutableArray *family = [[NSMutableArray alloc] init];

for (int i = 0; i < numberOfPeople; i++) {      
 // simulate person's attributes
 NSArray *keys = [[NSArray alloc] initWithObjects:
       @"id", 
       @"name",
       @"age",
       Nil];

 NSArray *values = [[NSArray alloc] initWithObjects:
         [NSNumber numberWithInt:i],
         @"Edward", 
         [NSNumber numberWithInt:10],
         Nil];

 // create a Person (a dictionary)
 NSDictionary *person = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
 [family addObject:person];
 [person release];  
}

// save the "person" object to the property list
[family writeToFile:@"<path>/family.plist" atomically:YES];
[family release];

这将生成 family.plist 文件.

但是请注意,我的 Person 对象实际上是一个NSDictionary对象(我认为子类化不是一个好主意),具有属性作为键. 有什么办法可以创建像这样的Objective-C类:

Note, however, that my Person object is actually a NSDictionary object (which I don't think subclassing would be a good idea) with attributes as keys. Is there any way to create a Objective-C class like this:

@interface Person : NSObject {
 NSString *_id; 
 NSString *_name;
 NSNumber *_age;
}
@end

并将其序列化为plist/XML文件? (我需要以文本格式(不是二进制)保存对象,以便可以在文本编辑器中编辑此类数据,并允许我的应用程序在运行时加载该数据,并正常转换为Objective-C对象.)

and serialize it to a plist/XML file? (I need to save the object in text format (not binary) so that I can edit such data in a text editor and allow my application to load this at runtime, converting to Objective-C objects normally).

预先感谢, 爱德华多

推荐答案

如果您还没有阅读它,我强烈建议您使用

If you haven't already read it, I would highly recommend the Archives and Serializations Programming Guide, particularly the parts about encoding and decoding objects using the NSCoding protocol.

如果使用该协议,则对象将存储为NSData.但是您可以模仿它(即创建自己的协议),以便您的类返回自己的字典表示形式,并可以从字典表示形式初始化自己(我认为没有办法自动做到这一点).但是,结果基本上与您发布的family.plist文件相同.

If you use that protocol, your objects will be stored as NSData. But you could imitate it (i.e. create your own protocol) so that your classes return dictionary representations of themselves, and can initialize themselves from a dictionary representation (I don't think there's a way to automatically do that). However, the result would essentially be the same as the family.plist file you posted.

如果您正在寻找一种获取XML文件的方法,

If you're looking for a way to get an XML file like this:

<person>
    <id>0</id>
    <name>Edward</name>
    <age>10</age>
</person>

然后plist不是您想要的.

then plist isn't what you want.

这篇关于使用Obj-C/iPhone SDK以XML格式进行对象序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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