存儲幾種方式
- XML屬性列表(plist)歸檔
- Preference(偏好設(shè)置)
- NSKeyedArchiver歸檔(必須遵循NSCoding協(xié)議)
- SQLite3
- Core Data
應(yīng)用沙河
- 每個iOS應(yīng)用都有自己的應(yīng)用沙盒(應(yīng)用沙盒就是文件系統(tǒng)目錄)搀缠,與其他文件系統(tǒng)隔離厌衔。應(yīng)用必須待在自己的沙盒里,其他應(yīng)用不能訪問該沙盒
-
應(yīng)用沙盒的文件系統(tǒng)目錄征讲,如下圖所示(假設(shè)應(yīng)用的名稱叫Layer)
Snip20160309_1.png - 模擬器應(yīng)用沙盒的跟路徑為:(apple是用戶名聊疲,8.0是模擬器版本)
/Users/apple/Library/Application Support/iPhone Simulator/8.0/Applications
應(yīng)用沙盒的結(jié)構(gòu)解析
- 應(yīng)用程序包:(上圖中的Layer)包含了所有的資源文件和可執(zhí)行文件
- Documents:保存應(yīng)用運行時生成的需要持久化的數(shù)據(jù)。iTunes同步設(shè)備時會備份該目錄。例如:游戲應(yīng)用的存檔
- tmp:保存運行時所需要的臨時文件筒严,使用完畢之后再講相應(yīng)的文件從該目錄刪除,應(yīng)用沒有運行時情萤,系統(tǒng)也可能將該目錄下的文件刪除萝风。(是真的不知道什么時候就刪了),iTunes不會同步備份該目錄
- Library/Caches:保存應(yīng)用運行時生成的需要持久化的數(shù)據(jù)紫岩,iTunes同步設(shè)備時不會備份规惰,但是系統(tǒng)也不會刪除,一般存儲體積較大泉蝌,又不用備份的暑假歇万,例如圖片揩晴,介紹文字什么的。
- Library/Preference:保存應(yīng)用所有的偏好設(shè)置贪磺,iOSDSettings(設(shè)置)應(yīng)用會在該目錄中查找應(yīng)用的設(shè)置信息硫兰。iTunes同步設(shè)備時會備份該目錄。
應(yīng)用沙盒目錄的常見獲取方式
沙盒根目錄:NSString *home = NSHomeDirectory();
Documents:(2種方式)
- 利用沙盒根目錄拼接”Documents”字符串
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];```
// 不建議采用寒锚,因為新版本的操作系統(tǒng)可能會修改目錄名
2. 利用NSSearchPathForDirectoriesInDomains函數(shù)
// NSUserDomainMask 代表從用戶的手機上查找
// YES 代表展開路徑中的波浪字符“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 在iOS中劫映,只有一個目錄跟傳入的參數(shù)匹配,所以這個集合里面只有一個元素
NSString *documents = [array objectAtIndex:0];
#### tmp
tmp:NSString *tmp = NSTemporaryDirectory();
#### Caches
Library/Caches:(跟Documents類似的2種方法)
利用沙盒根目錄拼接”Caches”字符串
利用NSSearchPathForDirectoriesInDomains函數(shù)(將函數(shù)的第2個參數(shù)改為:NSCachesDirectory即可)
#### Preference
Library/Preference:通過NSUserDefaults類存取該目錄下的設(shè)置信息
# plist
屬性列表是一種xml格式的文件刹前,拓展名后綴為.plist
#### 注意事項
- 不能存儲自定義的對象
- 對象為NSString泳赋、NSDictionary、NSArray喇喉、NSData祖今、NSNumber等類型
- 如何判斷一個對象能不能使用plist,就看下有沒有writeToFile方法就可以了,方法會將對象直接寫進屬性列表文件中
寫入代碼:
```objc
NSArray *arr = @[@"123",@1,p];
// 獲取應(yīng)用的文件夾(應(yīng)用沙盒)
// NSString *homePath = NSHomeDirectory();
// 獲取temp
// NSTemporaryDirectory();
// 獲取Cache文件路徑
// NSSearchPathDirectory:搜索的目錄
// NSSearchPathDomainMask:搜索范圍 NSUserDomainMask:表示在用戶的手機上查找
// expandTilde 是否展開全路徑拣技,如果沒有展開千诬,應(yīng)用的沙盒路徑就是~
// 存儲一定要要展開路徑
NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
// 這個就相當于創(chuàng)建個你要寫入的文件
NSString *filePath = [cachePaht stringByAppendingPathComponent:@"personArr.plist"];
NSLog(@"%@",cachePaht);
// File:文件的全路徑
[arr writeToFile:filePath atomically:YES];
讀取代碼
// 拿到展開的路徑
NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名,取到要讀取的文件
// 必須用stringByAppendingPathComponent膏斤,它會自動在你的拼接參數(shù)前加一個"/"
NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
// 將文件內(nèi)容編譯轉(zhuǎn)出來
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",arr);
偏好設(shè)置存儲
這個其實很簡單徐绑,但是用處很大,一般存儲APP的賬號密碼莫辨,登陸狀態(tài)泵三,字體大小等參數(shù),用處很大
- 好處:
- 不需要關(guān)心文件名
- 快速做鍵值對存儲
- 底層:就是封裝了一個字典衔掸,但是本身不是字典
存儲代碼
// 取到存儲的對象
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"xmg" forKey:@"account"];
[userDefaults setObject:@"123" forKey:@"pwd"];
[userDefaults setBool:YES forKey:@"rmbPwd"];
// 在iOS7之前,默認不會馬上把跟硬盤同步俺抽,會取到時間戳判定寫入敞映,所以不實時,因此為了防止還沒寫入磷斧,程序就終止了振愿,要加入下面這行代碼
// 同步
[userDefaults synchronize];
讀取代碼
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *pwd = [userDefaults objectForKey:@"pwd"];
NSLog(@"%@",pwd);
刪除代碼
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults removeObjectForKey:@"pwd"];
NSKeyedArchiver
- 如果對象是NSString、NSDictionary弛饭、NSArray冕末、NSData、NSNumber等類型侣颂,可以直接用NSKeyedArchiver進行歸檔和恢復(fù)
- 不是所有的對象都可以直接用這種方法進行歸檔档桃,必須遵守了NSCoding協(xié)議的對象才可以
NSCoding協(xié)議有2個方法:
- encodeWithCoder:
// 什么時候調(diào)用:自定義對象歸檔的時候,每次歸檔對象都會調(diào)
// 作用:用來描述當前對象里面的哪些屬性需要歸檔
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// 加下面這行代碼是為了保證繼承這個類的實例變量也能被編碼
[super encodeWithCode:encode];
// name
// encodeObject:用于歸檔實例變量
[aCoder encodeObject:_name forKey:@"name"];
// age
[aCoder encodeInt:_age forKey:@"age"];
}
- initWithCoder
// 什么時候調(diào)用:解檔對象的時候調(diào)用憔晒,每次從文件中恢復(fù)(解碼)對象時
// 作用:用來描述當前對象里面的哪些屬性需要解檔
// initWithCoder:就是用來解析文件的藻肄。
- (id)initWithCoder:(NSCoder *)aDecoder
{
// super:NSObject
self = [super init];
#warning 什么時候需要調(diào)用initWithCoder
if (self ) {
// 注意:一定要給成員變量賦值
// name
// decodeObjectForKey:這個方法用于解碼實例變量
_name = [aDecoder decodeObjectForKey:@"name"];
// age
_age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
歸檔方法
- 歸檔一般數(shù)據(jù)類型蔑舞,例如數(shù)組
// 歸檔數(shù)組對象的話:
// NSArray *array = [NSArray arrayWithObjects:@”a”,@”b”,nil];
// 獲取cache
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 獲取文件的全路徑
NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
// 把自定義對象歸檔
[NSKeyedArchiver archiveRootObject:p toFile:filePath];
}
- 歸檔自定義數(shù)據(jù)類型,例如Person對象
@interface Person : NSObject<NSCoding>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) float height;
@end
@implementation Person
- (void)encodeWithCoder:(NSCoder *)encoder {
[super encodeWithCode:encode];
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeInt:self.age forKey:@"age"];
[encoder encodeFloat:self.height forKey:@"height"];
}
- (id)initWithCoder:(NSCoder *)decoder {
self.name = [decoder decodeObjectForKey:@"name"];
self.age = [decoder decodeIntForKey:@"age"];
self.height = [decoder decodeFloatForKey:@"height"];
return self;
}
@end
- (void)save {
Person *p = [[Person alloc] init];
p.age = 18;
// 獲取cache
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 獲取文件的全路徑
NSString *filePath = [cachePath stringByAppendingPathComponent:@"array.data"];
// 把自定義對象歸檔
[NSKeyedArchiver archiveRootObject:array toFile:filePath];
}
讀取方法
- (void)read {
// 獲取cache
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 獲取文件的全路徑
NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
// 解檔
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d",p.age);
}
多文件存儲在同一個文件中
- 使用archiveRootObject:toFile:方法可以將一個對象直接寫入到一個文件中嘹屯,但有時候可能想將多個對象寫入到同一個文件中攻询,那么就要使用NSData來進行歸檔對象
- NSData可以為一些數(shù)據(jù)提供臨時存儲空間,以便隨后寫入文件州弟,或者存放從磁盤讀取的文件內(nèi)容钧栖。可以使用[NSMutableData data]創(chuàng)建可變數(shù)據(jù)空間
Snip20160309_4.png
歸檔
// 新建一塊可變數(shù)據(jù)區(qū)
NSMutableData *data = [NSMutableData data];
// 將數(shù)據(jù)區(qū)連接到一個NSKeyedArchiver對象
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
// 開始存檔對象婆翔,存檔的數(shù)據(jù)都會存儲到NSMutableData中
[archiver encodeObject:person1 forKey:@"person1"];
[archiver encodeObject:person2 forKey:@"person2"];
// 存檔完畢(一定要調(diào)用這個方法)
[archiver finishEncoding];
// 將存檔的數(shù)據(jù)寫入文件
[data writeToFile:path atomically:YES];
恢復(fù)(解碼)
// 從文件中讀取數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfFile:path];
// 根據(jù)數(shù)據(jù)拯杠,解析成一個NSKeyedUnarchiver對象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
Person *person1 = [unarchiver decodeObjectForKey:@"person1"];
Person *person2 = [unarchiver decodeObjectForKey:@"person2"];
// 恢復(fù)完畢
[unarchiver finishDecoding];
利用歸檔實現(xiàn)深復(fù)制
簡單來說就是復(fù)制兩個實例對象罷了
比如對一個Person對象進行深復(fù)制
// 臨時存儲person1的數(shù)據(jù)
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:person1];
// 解析data,生成一個新的Person對象
Student *person2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
// 分別打印內(nèi)存地址
NSLog(@"person1:0x%x", person1); // person1:0x7177a60
NSLog(@"person2:0x%x", person2); // person2:0x7177cf0
Snip20160309_5.png