一、本地持久化
所謂的持久化,就是將數(shù)據(jù)保存到硬盤中,使得在應(yīng)用程序或機(jī)器重啟后可以繼續(xù)訪問之前保存的數(shù)據(jù)。在iOS開發(fā)中,有很多數(shù)據(jù)持久化的方案,接下來我將嘗試著介紹一下5種方案:
plist文件(序列化)
preference(偏好設(shè)置)
NSKeyedArchiver(歸檔)
SQLite3
CoreData
本節(jié)我就只寫關(guān)于歸檔、偏好設(shè)置和屬性列表的,這些都是小數(shù)據(jù)持久化常用的方式,SQLite3和CoreData都是數(shù)據(jù)庫大數(shù)據(jù)存儲(chǔ)的方式,以后再寫。
二、沙盒
在介紹各種存儲(chǔ)方法之前,有必要說明以下沙盒機(jī)制。iOS程序默認(rèn)情況下只能訪問程序自己的目錄,這個(gè)目錄被稱為“沙盒”。
既然沙盒就是一個(gè)文件夾,那就看看里面有什么吧。沙盒的目錄結(jié)構(gòu)如下:
應(yīng)用程序包:存放的是應(yīng)用程序的源文件,包括資源文件和可執(zhí)行文件
NSString *path =[[NSBundle mainBundle]bundlePath];
Documents:最常用的目錄,iTunes同步該應(yīng)用時(shí)會(huì)同步此文件夾中的內(nèi)容,適合存儲(chǔ)重要數(shù)據(jù)
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
Library
Caches:iTunes不會(huì)同步此文件夾,適合存儲(chǔ)體積大,不需要備份的非重要數(shù)據(jù)。
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES).firstObject;
Preferences:iTunes同步該應(yīng)用時(shí)會(huì)同步此文件夾中的內(nèi)容,通常保存應(yīng)用的設(shè)置信息。
tmp: iTunes不會(huì)同步此文件夾,系統(tǒng)可能在應(yīng)用沒運(yùn)行時(shí)就刪除該目錄下的文件,所以此目錄適合保存應(yīng)用中的一些臨時(shí)文件,用完就刪除。
NSString*path =NSTemporaryDirectory();
三、plist文件(序列化)
plist文件是將某些特定的類,通過XML文件的方式保存在目錄中。
可以被序列化的類型只有如下幾種:
NSArray;//數(shù)組NSMutableArray;//可變數(shù)組NSDictionary;//字典NSMutableDictionary;//可變字典NSData;//二進(jìn)制數(shù)據(jù)NSMutableData;//可變二進(jìn)制數(shù)據(jù)NSString;//字符串NSMutableString;//可變字符串NSNumber;//基本數(shù)據(jù)NSDate;//日期
以NSArray為例,其他類使用方法完全相似:
- (void)writeToPlist{NSString*path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;NSString*fileName = [path stringByAppendingPathComponent:@"123.plist"];NSArray*array = @[@"1234",@"tony",@"java"];//序列化,把數(shù)組存入plist文件[array writeToFile:fileName atomically:YES];}- (NSArray*)readFromPlist{NSString*path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;NSString*fileName = [path stringByAppendingPathComponent:@"123.plist"];//反序列化,把plist文件數(shù)據(jù)讀取出來,轉(zhuǎn)為數(shù)組NSArray*result = [NSArrayarrayWithContentsOfFile:fileName];NSLog(@"%@", result);returnresult;}
存儲(chǔ)時(shí)使用writeToFile:atomically:方法。 其中atomically表示是否需要先寫入一個(gè)輔助文件,再把輔助文件拷貝到目標(biāo)文件地址。這是更安全的寫入文件方法,一般都寫YES。

plist文件圖示
四、Preference(偏好設(shè)置)
偏好設(shè)置是專門用來保存應(yīng)用程序的配置信息的,一般不要在偏好設(shè)置中保存其他數(shù)據(jù)。
偏好設(shè)置是以key-value的方式進(jìn)行存儲(chǔ)和讀寫,使用到一個(gè)單例對(duì)象NSUserDefaults
下面是使用方法:
//1.獲得NSUserDefaults文件NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];//2.向偏好設(shè)置中寫入內(nèi)容[userDefaultssetObject:@"AAA"forKey:@"a"];[userDefaultssetBool:YESforKey:@"sex"];[userDefaultssetInteger:21forKey:@"age"];//2.1立即同步[userDefaults synchronize];//3.讀取偏好設(shè)置NSString *name = [userDefaults objectForKey:@"a"];BOOL sex = [userDefaults boolForKey:@"sex"];NSInteger age = [userDefaults integerForKey:@"age"];NSLog(@"%@, %d, %ld", name, sex, age);
如果沒有調(diào)用synchronize方法,系統(tǒng)會(huì)根據(jù)I/O情況不定時(shí)刻地保存到文件中。所以如果需要立即寫入文件的就必須調(diào)用synchronize方法。
偏好設(shè)置會(huì)將所有數(shù)據(jù)保存到同一個(gè)文件中,使用同一個(gè)key進(jìn)行存儲(chǔ)數(shù)據(jù),會(huì)覆蓋之前存儲(chǔ)的數(shù)據(jù)。
五、歸檔和解檔
歸檔在iOS中是另一種形式的序列化,只要遵循了NSCoding協(xié)議的對(duì)象都可以通過它實(shí)現(xiàn)序列化。由于絕大多數(shù)支持存儲(chǔ)數(shù)據(jù)的類都遵循了NSCoding協(xié)議,因此,對(duì)于大多數(shù)類來說,歸檔相對(duì)而言還是比較容易實(shí)現(xiàn)的。
1. 遵循NSCoding協(xié)議
NSCoding協(xié)議聲明了兩個(gè)方法,這兩個(gè)方法都是必須實(shí)現(xiàn)的:
encodeWithCoder:用來說明如何將對(duì)象編碼到歸檔中。
initWithCoder:用來說明如何進(jìn)行解檔來獲取一個(gè)新對(duì)象。
//1.遵循NSCoding協(xié)議@interfacePerson:NSObject//2.設(shè)置屬性@property(strong,nonatomic)UIImage*avatar;@property(copy,nonatomic)NSString*name;@property(assign,nonatomic)NSIntegerage;@end@implementationPerson//解檔- (id)initWithCoder:(NSCoder*)aDecoder {if(self= [superinit]) {self.avatar = [aDecoder decodeObjectForKey:@"avatar"];self.name = [aDecoder decodeObjectForKey:@"name"];self.age = [aDecoder decodeIntegerForKey:@"age"];? ? ? }returnself;}//歸檔- (void)encodeWithCoder:(NSCoder*)aCoder {? ? ? [aCoder encodeObject:self.avatar forKey:@"avatar"];? ? ? [aCoder encodeObject:self.name forKey:@"name"];? ? ? [aCoder encodeInteger:self.age forKey:@"age"];}@end
2. NSKeyedArchiver歸檔
調(diào)用NSKeyedArchiver的archiveRootObject:toFile:方法把對(duì)象歸檔
NSString*path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;NSString*file = [path stringByAppendingPathComponent:@"person.data"];Person *person = [[Person alloc] init];person.avatar = [UIImageimageNamed:@"icon.png"];person.name =@"Xiao Ming";person.age =17;[NSKeyedArchiverarchiveRootObject:person toFile:file];
3. NSKeyedUnarchiver解檔
調(diào)用NSKeyedUnarchiver的unarchiveObjectWithFile:方法從文件中解檔對(duì)象
NSString*path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;NSString*file = [path stringByAppendingPathComponent:@"person.data"];Person *person = [NSKeyedUnarchiverunarchiveObjectWithFile:file];if(person) {self.avatarView.image = person.avatar;self.nameField.text = person.name;self.ageField.text = [NSStringstringWithFormat:@"%ld", person.age];}
必須遵循并實(shí)現(xiàn)NSCoding協(xié)議
保存文件的擴(kuò)展名可以任意指定
繼承時(shí)必須先調(diào)用父類的歸檔解檔方法,這里因?yàn)楦割愂荖SObject,就不用了