如何在Objective-C中将我的Class与PList一起使用? [英] How to use my Class with PList in objective-c?

查看:44
本文介绍了如何在Objective-C中将我的Class与PList一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于处理项目中数据的类,现在需要存储数据.我想使用一个Plist,但是我不确定如何开始.我的课很简单-6条数据,固定(无层次结构).

I have a Class for handling my data in my project, and now I need to store the data. I'd like to use a Plist but I'm a bit unsure of how to start. My class is pretty simple - 6 pieces of data, flat (no hierarchy).

我希望我的应用程序不包含任何数据,所以我可以假设用户创建第一条数据后,我应该以编程方式创建PList吗?(也就是说,在分发之前,不要在支持文件"中创建.plist文件吗?)

I want my app to start with no data, so can I assume that I should create the PList programmatically once the User creates their first piece of data? (That is, don't create a .plist file in 'Supporting Files' prior to distribution?)

然后,当应用程序下次启动时,读取数据并创建Class实例的NSMUtableArray数组吗?

Then, when the app starts the next time, read the data and create an NSMUtableArray array of Class instances?

推荐答案

要创建属性列表,您需要做的就是使用适当的类型(即那些支持属性列表格式的类型:NSData,NSString,NSDictionary,NSNumber,NSDate,NSArray),将它们存储在单个容器中,并告诉包含对象将自身写入文件.要读取数据,可以使用路径初始化相同的类型.例如:

To create a property list, all you need to do is use appropriate types (i.e. those that support the property list format: NSData, NSString, NSDictionary, NSNumber, NSDate, NSArray), store them in a single container, and tell the containing object to write itself to a file. To read the data, you can initialize that same type using a path. For example:

// writing some data to a property list
NSString *somePath = ... // replace ... with the path where you want to store the plist file
NSMutableDictionary myDict = [NSMutableDictionary dictionary];
[myDict setObject:@"Caleb" forKey:@"name"];
[myDict setObject:[NSNumber numberWithInt:240] forKey:@"cholesterolOrIQ"];
[myDict writeToFile:somePath atomically:YES];

// reading the file again
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:somePath];

这篇关于如何在Objective-C中将我的Class与PList一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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