[英]Best way to save data in iPod Touch/iPhone Objective-C
I am writing a very simple application, for the iPhone. 我正在为iPhone编写一个非常简单的应用程序。 Unfortunately I am really a newbie.
不幸的是我真的是一个新手。 What I am trying to do is to save data at the end of a user experience.
我想做的是在用户体验结束时保存数据。 This data is really simple, only string or int, or some array.
这个数据真的很简单,只有字符串或整数,或者是一些数组。
Later I want to be able to retrieve that data, therefore I also need an event ID (I suppose). 稍后,我希望能够检索该数据,因此,我还需要一个事件ID(我想)。
Could you please point out the best way, API or technology to achieve that, XML, plain text, serialization... ? 您能否指出实现此目标的最佳方法,API或技术,XML,纯文本,序列化...?
Use NSUserDefaults
. 使用
NSUserDefaults
。 Straight forward and easy to use. 直截了当,易于使用。 This will handle all File I/O for you, and takes only a couple lines of code.
这将为您处理所有文件I / O,并且只需要几行代码。
NSUserDefaults
is a good choice for small amounts of data. NSUserDefaults
是处理少量数据的不错选择。
If you need to manage something larger than a kilobyte or so, you might consider using the initWithContentsOfFile:
methods of NSArray
or NSDictionary
to read in a .plist file. 如果您需要管理大于一千字节左右的内容,则可以考虑使用
NSArray
或NSDictionary
的initWithContentsOfFile:
方法来读取.plist文件。 To write the file, use the writeToFile:atomically:
methods. 要写入文件,请使用
writeToFile:atomically:
方法。 This file needs to go in your app's documents directory. 该文件需要进入您应用程序的文档目录中。
Me and my team created a simple class for this purpose which as Mark pointed out makes use of NSUserDefaults
. 我和我的团队为此目的创建了一个简单的类,正如Mark指出的那样,它使用
NSUserDefaults
。 Hopefully this helps you out... 希望这可以帮助您...
http://getsetgames.com/2009/10/07/saving-and-loading-user-data-and-preferences/ http://getsetgames.com/2009/10/07/saving-and-loading-user-data-and-preferences/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.