1、Property Lists
屬性列表,擴(kuò)展名為plist。
- Property list有三種存儲(chǔ)方式:XML, binary format, “old-style” ASCII format inherited from OpenStep。old-style format的序列化API是只讀的。
A property list can be stored in one of three different ways: in an XML representation, in a binary format, or in an “old-style” ASCII format inherited from OpenStep. You can serialize property lists in the XML and binary formats. The serialization API with the old-style format is read-only.
- 數(shù)據(jù)大小限制(幾百kb內(nèi))
For situations where you need to store small amounts of persistent data—say less than a few hundred kilobytes—property lists offer a uniform and convenient means of organizing, storing, and accessing the data.
In some situations, the property-list architecture may prove insufficient. If you need a way to store large, complex graphs of objects, objects not supported by the property-list architecture, or objects whose mutability settings must be retained, use archiving.
如果我們的對(duì)象全由NSDictionary, NSArray, NSString, NSDate, NSData, 或NSNumber類派生,我們可以創(chuàng)建property list。
XML property list 比 binary 格式的可移植性好,可以手動(dòng)編輯,但是binary property lists 兼容性好,需要的存儲(chǔ)空間少,讀寫速度要優(yōu)于XML property lists。通常來(lái)說(shuō),如果你的property list相對(duì)較小,XML property lists的優(yōu)點(diǎn)會(huì)戰(zhàn)勝它相對(duì)binary property lists的I/O速度,難兼容的缺點(diǎn)。
如果你有大量數(shù)據(jù),binary property lists, keyed archives,或自定義數(shù)據(jù)格式會(huì)是更好的解決方案。
2、NSUserDefaults
NSUserDefaults,提供了與默認(rèn)系統(tǒng)交互的程序接口。
默認(rèn)系統(tǒng)允許應(yīng)用去自定義它的行為來(lái)符合用戶的偏好。所以它主要是用來(lái)存儲(chǔ)配置信息。
支持的數(shù)據(jù)類型有:floats,doubles,integers,Booleans,URLs,NSData,NSString,NSNumber,NSDate,NSArray和NSDictionary。如果還想存儲(chǔ)其他類型的對(duì)象,需要使用NSData對(duì)它們進(jìn)行歸檔。
事實(shí)上,NSUserDefaults就是把數(shù)據(jù)保存在property list中
3、Archive
Archives provide a means to convert objects and values into an architecture-independent stream of bytes that preserves the identity of and the relationships between the objects and values.
如果對(duì)象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,可以直接用NSKeyedArchiver進(jìn)行歸檔和恢復(fù)。
不是所有的對(duì)象都可以直接用這種方法進(jìn)行歸檔,只有遵守了NSCoding協(xié)議的對(duì)象才可以。
3、DB
數(shù)據(jù)庫(kù)可以提供對(duì)數(shù)據(jù)更加高級(jí)的操作,這是其他方式不能提供的。當(dāng)涉及到建模,查詢,遍歷,持久化復(fù)雜的對(duì)象圖,就可以采用數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn)數(shù)據(jù)持久化。
- SQLite
- Core Data
- FMDB