iOS存储只是一点点数据 [英] iOS store just a little bit of data

查看:76
本文介绍了iOS存储只是一点点数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法来存储少量的数据,而不需要一个完整的核心数据API。



谢谢你,Alex。




  • 使用 NSUserDefaults 。 (死简单。)


  • 将数据存储在适当的结构中(例如 NSDictionary 它作为属性列表。


  • 将数据存储在自己设计的类中,实现 NSCoding ,然后使用 NSKeyedArchiver 将该类的实例写入文件。 (适用于存储整个对象图;这基本上是IB做的。光可能需要一两个小时才能完成,但是一旦你理解它,这是一个非常好的方式来读取和写入对象。)


  • 使用Cocoa Touch的文件系统API,特别是 NSFileHandle NSFileManager


  • 使用普通的旧的POSIX文件系统API(如果你之前使用过文件系统, (最适合现有的Unix代码,或者您也希望在其他平台上编译的代码。)




到任何一个,请阅读苹果 归档和序列化编程指南 用户默认设置主题 文件系统编程指南


I was wondering if there is a way to store small amounts of data, without going to a full-blown core-data API. I just need to store 6 'double' values somewhere... What's the best approach for that?

Thanks, Alex.

解决方案

Core Data is just one way to store data, and it only makes sense when you need the things that it does. Here are five good options for storing your data:

  • Use NSUserDefaults. (Dead simple.)

  • Store the data in an appropriate structure (say, NSDictionary) and store it as a property list. (Pretty darn easy.)

  • Store the data in a class of your own design that implements NSCoding, and then write an instance of that class to a file using NSKeyedArchiver. (Works well for storing entire object graphs; this is basically what IB does. It might take an hour or two for the light to come on, but once you understand it this is a very nice way to read and write objects.)

  • Use Cocoa Touch's file system API, notably NSFileHandle and NSFileManager. (Conceptually simple if you've ever worked with a file system before. Puts you in complete control.)

  • Use the regular old POSIX file system API. (Best for existing Unix code, or code that you also want to compile on other platforms.)

Before you jump into any of those, read Apple's Archives and Serializations Programming Guide, User Defaults Programming Topics, and File System Programming Guide.

这篇关于iOS存储只是一点点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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