iOS沙盒機制
iOS應用程序只能在為該改程序創(chuàng)建的文件系統(tǒng)中讀取文件,不可以去其它地方訪問,此區(qū)域被成為沙盒,所以所有的非代碼文件都要保存在此,例如圖像,圖標,聲音,映像,屬性列表,文本文件等。
1、每個應用程序都有自己的存儲空間
2、應用程序不能翻過自己的圍墻去訪問別的存儲空間的內(nèi)容
3、應用程序請求的數(shù)據(jù)都要通過權(quán)限檢測,假如不符合條件的話,不會被放行。
沙盒主路徑
程序運行期間,系統(tǒng)會生成一個專屬的沙盒路徑,應用程序在使用期間的非代碼文件都存儲在當前的沙盒路徑里面
NSString *homePath = NSHomeDirectory();
NSLog(@"%@",homePath);
通過Xcode打印出來的路徑,粘貼到finder中的前往文件夾就查看到了目錄
目錄結(jié)構(gòu)
默認情況下,每個沙盒有三個文件夾:Documents,Library和tmp. 因為應用的沙盒機制,應用智能在幾個目錄下讀寫文件
Documents:用來存儲永久性的數(shù)據(jù)的文件,程序運行時必要的文件都存儲在這里(數(shù)據(jù)庫),itunses會自動備份這里的文件
Library:用于保存程序運行期間生成的文件;
Library/Caches:用于保存程序運行期間 產(chǎn)生的緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除
tmp:提供一個即時創(chuàng)建臨時文件的地方,程序運行期間產(chǎn)生的臨時碎片會保存到這個文件夾里,通常文件下載完之后或者程序退出會自動清空此文件夾,ituns不會備份這里的數(shù)據(jù)。
簡單對象寫入文件
NSString寫入文件
//1.準備字符串
NSString *string = @"we are family";
//2.準備路徑
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"咒語.txt"];
//3.寫入文件
//第一個參數(shù):路徑
//第二個參數(shù):是否進行線性操作(YES的時候,保證發(fā)生意外時有中轉(zhuǎn)文件來保存信息,直至寫入完成,但是損耗大; NO的時候,寫入速度快,但是沒有安全保證)
//第三個參數(shù):編碼方式
//第四個參數(shù):錯誤對象
[string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//4.打印路徑
NSLog(@"%@",path);
// //讀取文件
// //第一個參數(shù):路徑
// //第二個參數(shù):編碼方式(切記要和寫入時用相同的編碼方式)
// //第三個參數(shù):錯誤信息
NSString *contentString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",contentString);
NSArray寫入文件
NSArray *array = [NSArray arrayWithObjects:@"Lily",@"Yucui",@"Star",@"Ling",@"WenQI",@"Yangyang", nil];
NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
docuPath = [docuPath stringByAppendingString:@"/Lady.txt"];
//寫入文件
[array writeToFile:docuPath atomically:YES];
NSLog(@"%@",docuPath);
//讀取文件
NSArray *array1 = [NSArray arrayWithContentsOfFile:docuPath];
NSLog(@"%@",array1);
NSDicitionary寫入文件
NSString *path1 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSString *path2 = [path1 stringByAppendingString:@"/PreferencePans"];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"HK",@"hkhk",@"jiji",@"ji",@"kiki",@"ki", nil];
NSLog(@"%@",path2);
//寫入
[dic writeToFile:path2 atomically:YES];
//讀取
NSDictionary *dic1 = [NSDictionary dictionaryWithContentsOfFile:path2];
NSLog(@"%@",dic1);
```
***NSData寫入文件***
//1.獲取圖片對象
UIImage *image = [UIImage imageNamed:@"999.png"];
//2.準備路徑
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"999.png"];
//3.將圖片對象轉(zhuǎn)換成Data
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:homePath atomically:YES];
NSLog(@"%@",homePath);
//4.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.image = [UIImage imageWithContentsOfFile:homePath];
[self.view addSubview:imgView];
#復雜對象寫入文件
簡單對象可以通過writeToFile寫入的方式,但是復雜對象沒有writeToFile的方法寫入文檔,所以我們需要借助歸檔和反歸檔的方法,將復雜對象轉(zhuǎn)換成簡單對象,寫入文檔
SingleVip *vip = [SingleVip new];
vip.name = @"法特";
vip.assets = @"數(shù)不清";
vip.car = @"蘭博基尼";
vip.age = 18;
//準備路徑
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"鉆石王老五.txt"];
//創(chuàng)建數(shù)據(jù)對象,用來存放vip對象
NSMutableData *data = [NSMutableData data];
//創(chuàng)建歸檔對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//開始歸檔
[archiver encodeObject:vip forKey:@"vip"];
//完成歸檔
[archiver finishEncoding];
//寫入文件
[data writeToFile:homePath atomically:YES];
NSLog(@"%@",homePath);
//反歸檔
//將文件里的data對象讀取出來
NSData *_data = [NSData dataWithContentsOfFile:homePath];
//創(chuàng)建反歸檔對象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:_data];
SingleVip *_vip = [unarchiver decodeObjectForKey:@"vip"];
//完成反歸檔
[unarchiver finishDecoding];
NSLog(@"%@",_vip.name);
注意: 歸檔并不是數(shù)據(jù)持久化,而是輔助復雜對象轉(zhuǎn)化成簡單對象的一種方式,真正實現(xiàn)數(shù)據(jù)持久化的仍然是writeTOFile 寫入文件
數(shù)據(jù)持久化的方式有:
plist(屬性列表)
偏好設(shè)置 NSUserDefaults(單列)
writeToFile 寫入文件
SQLite 數(shù)據(jù)庫
CoreData