沙盒目錄是一種數(shù)據(jù)安全策略,設(shè)計(jì)原理是只允許自己的應(yīng)用訪問(wèn)目錄,而不允許其他應(yīng)用訪問(wèn),只有特定的API共享特定的數(shù)據(jù)。目錄結(jié)構(gòu)如下:
|---------------Documents: 用于用戶生成的大文件或需要非常頻繁更新的數(shù)據(jù),能夠進(jìn)行iTunes或iCloud備份。(游戲進(jìn)度,寫(xiě)的日記什么的)
| /----------List.sqlite3
|---------------Library:
| /----------Caches 用于存放網(wǎng)絡(luò)獲取的數(shù)據(jù)。(音樂(lè)圖片緩存)(不會(huì)iCloud備份)
| /----------PReferences 用戶偏好設(shè)置(iCloud備份)NSUserDefault
|---------------tmp: 臨時(shí)存儲(chǔ)。(手機(jī)存儲(chǔ)不夠系統(tǒng)會(huì)去刪除里面的東西)
|---------------YourApp.app: bundle包。(程序包資源)
目錄獲取方式:
NSString *homePath = NSHomeDirectory();輸出路徑:
/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF2.Documents路徑:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;輸出路徑:
/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/Documents3.Library/Caches路徑:
NSString *libCachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;輸出路徑:/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/Library/Caches(Library/Preferences 由NSUserDefaults管理)4.tmp路徑:
NSString *tmpPath = NSTemporaryDirectory();輸出路徑:/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Data/Application/1B4DD145-0086-42F0-8B6D-1ABBACC2F0CF/tmp/5.bundle包路徑:
NSString *bundlePath = [NSBundle mainBundle].bundlePath;輸出路徑:/Users/jiling/Library/Developer/CoreSimulator/Devices/47450F88-22B6-41FD-8CB0-09FFEA47C551/data/Containers/Bundle/Application/1E871CFC-2F8A-4032-ADB2-F1C797AF8D11/HelloWorld.app二. NSFileManager 和 NSFileHandle
就像類名字顯示的,Manger用于管理文件,創(chuàng)建文件,刪除文件,Handle用于處理文件,改變文件的內(nèi)容。
1.NSFileManager--文件創(chuàng)建
//文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"hello.txt"; tmpPath = [tmpPath stringByAppendingPathComponent:fileName]; //這個(gè)方法會(huì)監(jiān)測(cè)是否用加'/',用來(lái)合成path //創(chuàng)建文件 NSFileManager *fileManager = [NSFileManager defaultManager]; if(![fileManager fileExistsAtPath:tmpPath]){ //判斷下文件是否已經(jīng)存在 BOOL flag = [fileManager createFileAtPath:tmpPath contents:nil attributes:nil]; if(flag){ NSLog(@"文件創(chuàng)建成功"); }else{ NSLog(@"文件創(chuàng)建失敗"); } }2.NSFileManager--創(chuàng)建目錄
//目錄路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *directorName = @"hello"; NSString *path = [tmpPath stringByAppendingPathComponent:directorName]; //創(chuàng)建目錄 NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL flag = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; if(flag){ NSLog(@"創(chuàng)建目錄成功"); }else{ NSLog(@"創(chuàng)建目錄失敗"); }3.NSFileManager--刪除文件和目錄
NSFileManager *fileManager = [NSFileManager defaultManager]; //目錄路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *directorName = @"test1"; NSString *path = [tmpPath stringByAppendingPathComponent:directorName]; //刪除文件 NSArray *array = [fileManager contentsOfDirectoryAtPath:path error:nil]; for(NSString * str in array){ //循環(huán)刪除test2下所有文件及目錄 NSString *filePath = [path stringByAppendingPathComponent:str]; BOOL flag = [fileManager removeItemAtPath:filePath error:nil]; if(flag){ NSLog(@"刪除成功"); }else{ NSLog(@"刪除失敗"); } }4.NSFileManager--其它方法
//將一個(gè)文件復(fù)制到另一個(gè)文件 [fileManager copyItemAtPath:path1 toPath:path2 error:nil];//將一個(gè)文件移動(dòng)到另一個(gè)文件 [fileManager moveItemAtPath:path1 toPath:path2 error:nil];//獲取文件里面的內(nèi)容 NSData * readData=[fileManager contentsAtPath:path]5.NSFileHandle--文件寫(xiě)入
//文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"hello.txt"; NSString *path = [tmpPath stringByAppendingPathComponent:fileName]; NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; //根據(jù)路徑初始化handle NSString *str = @"hello"; //要寫(xiě)入的內(nèi)容 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:data]; //寫(xiě)數(shù)據(jù) [fileHandle closeFile]; //用完之后一定要關(guān)閉文件 在已有內(nèi)容,移動(dòng)光標(biāo)位置輸入://文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"hello.txt"; NSString *path = [tmpPath stringByAppendingPathComponent:fileName]; NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; //根據(jù)路徑初始化handle NSString *str = @" world"; //要寫(xiě)入的內(nèi)容 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle seekToEndOfFile]; //將光標(biāo)移到文件結(jié)尾 [fileHandle writeData:data]; //寫(xiě)數(shù)據(jù) [fileHandle closeFile]; //用完之后一定要關(guān)閉文件6.NSFileHandle--文件讀入
//文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"hello.txt"; NSString *path = [tmpPath stringByAppendingPathComponent:fileName]; NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; //根據(jù)路徑初始化handle NSData *data1 = [fileHandle readDataToEndOfFile]; //讀取到文件的末尾 [fileHandle availableData]; //查詢?cè)撐募捎脭?shù)據(jù)的個(gè)數(shù) NSData * data3 = [fileHandle readDataOfLength:3]; //讀取指定長(zhǎng)度的文件 [fileHandle closeFile];三. iOS數(shù)據(jù)存儲(chǔ)常用方式
1.plist文件讀寫(xiě)
用于存儲(chǔ)常用OC數(shù)據(jù)類型,要實(shí)現(xiàn)writeToFile方法的類型。
寫(xiě)入數(shù)據(jù)-NSArray:
//文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"str1.plist"; NSString *path = [tmpPath stringByAppendingPathComponent:fileName]; //寫(xiě)入數(shù)據(jù) NSArray *array = @[@"a",@"b",@"c"]; [array writeToFile:path atomically:YES]; //如果沒(méi)有這個(gè)文件會(huì)創(chuàng)建文件,如果已經(jīng)存在,會(huì)覆蓋文件查看下結(jié)果:
寫(xiě)入數(shù)據(jù)-NSDictionary:
//文件路徑 NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = @"str.plist"; NSString *path = [tmpPath stringByAppendingPathComponent:fileName]; //寫(xiě)入數(shù)據(jù) NSDictionary *dic = @{@"aa":@"1",@"bb":@"2",@"cc":@"3"}; [dic writeToFile:path atomically:YES];查看下結(jié)果:
讀plist數(shù)據(jù),例:
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];2.Preferences偏好數(shù)據(jù)
每個(gè)應(yīng)用都有一個(gè) NSUserDefault 實(shí)例,用來(lái)存取偏好設(shè)置,例如:用戶名,字體,登錄方式狀態(tài)等。不需要關(guān)心文件名稱,鍵值對(duì)存取。實(shí)際存儲(chǔ)形式為plist,所以依然只能存plist支持的類型,NSData, NSString, NSNumber, NSDate, NSArray, NSDictionary。不支持的類型,如圖片,可以先轉(zhuǎn)成NSData,再存。
NSUserDefault也為我們提供了一些定制好的方法,如下:
- setBool:forKey: - setFloat:forKey: - setInteger:forKey: - setDouble:forKey: - setURL:forKey:存入:
//獲取設(shè)置單例 NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults]; // 1、寫(xiě)入---系統(tǒng)會(huì)去存儲(chǔ)數(shù)據(jù) [UserDefaults setBool:NO forKey:@"isLogined"]; // 2、強(qiáng)制寫(xiě)入---當(dāng)需要立即存儲(chǔ)的時(shí)候 [UserDefaults synchronize];獲取://獲取設(shè)置單例 NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults]; BOOL isLogined = [UserDefaults objectForKey:@"isLogined"];注:有關(guān)NSUserDefaults域的知識(shí),總結(jié)好會(huì)加上來(lái)。3.NSKeyedArchiver歸檔
相對(duì)于plist,NSKyeyedArchiver可以存儲(chǔ)自定義模型。相對(duì)于SQLite,NSKyeyedArchiver不適合存儲(chǔ)大批量數(shù)據(jù),在讀取某一數(shù)據(jù)時(shí),要把文件中所有數(shù)據(jù)都讀取,再尋找,如果數(shù)據(jù)量過(guò)大,性能堪憂。
注:要?dú)w檔的模型,必須遵循NSCoding協(xié)議,然后再模型中必須實(shí)現(xiàn)兩個(gè)方法,
歸檔:encodeWithCoder
解檔:initWithCoder
如果父類也遵循NSCoding,那么注意這兩句的添加,[super encodeWithCoder:encode]; self = [super initWithCoder:decoder];使用例子:
4.SQLite3
SQLite是一款輕型的嵌入式數(shù)據(jù)庫(kù),占用的資源低,速度快。
傳送門(mén):(后續(xù))
5.Core Data
傳送門(mén): (后續(xù))
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注