沙盒(sandBox)
1.沙盒機制:它是一種安全體系,iOS應用程序只能對自己創建的程序進行讀取文件,這個獨立,封閉,安全的空間,就叫沙盒。它裏面一般存放着程序需要的文件,數據持久化的一些文件,只要不是代碼相關的都會放在裏面。
2.每個一個用程序只有一個沙盒,沙盒有三個文件夾
➕:獲取根目錄的方式
NSString *homePath = NSHomeDirectory();
1⃣️:Documents 目錄下的內容會被同步到另一臺設備,可以存放媒體資源,文本資源
➕:獲取Documents的目錄
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPaths = [documentsPath firstObject];
NSLog(@"%@",documentsPaths);
2⃣️:Library 可以把緩存的內容放到這裏面 Library/Caches
➕:獲取Library的方式
NSArray *librayPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPsths = [librayPath lastObject];
NSLog(@“%@",libraryPsths);
➕:獲取Caches的目錄
NSArray *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPaths = [cachesPath firstObject];
NSLog(@"%@",cachesPaths);
3⃣️: tmp 這是一個臨時目錄,當我們的設備重啓時,文件會被自動清除
➕:獲取tmp的目錄
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@",tmpPath);*/
歸檔
1.歸檔: NSKeyedArchiver 又叫序列化(Coding) 歸檔後的文件會被加密
2.可以歸檔的文件、數據:任何數據,文件(直接歸檔一個類實例化出來的對象,數組,字典,字符串,文本,圖片。。。。)
3.歸檔步驟:
1⃣️: 歸檔路徑,需要歸檔的數據
2⃣️:歸檔
➕:歸檔實例:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath =[ [path firstObject]stringByAppendingPathComponent:@“userList.archiver”];
// 前兩句是文件的路徑
NSArray *users = @[@"葫蘆娃",@"喵喵",@"汪汪"];
BOOL success = [NSKeyedArchiver archiveRootObject:users toFile:documentPath];
// 歸檔
if (success) {
[self showAlertWithMessage:@"歸檔成功"];
}
NSLog(@"%@",anotherPath);
4.解歸檔步驟:
解檔(解歸檔):NSKeyedUnarchiver
1⃣️:解歸檔的文件路徑
2⃣️:解歸檔
➕:解歸檔實例
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path1 = [[paths lastObject] stringByAppendingPathComponent:@“userList.archiver"];
// 解歸檔文件路徑
NSArray *ary = [NSKeyedUnarchiver unarchiveObjectWithFile:path1];
// 解歸檔
第二種歸檔方式
NSData : 二進制數據的類
1.歸檔步驟:
1⃣️:歸檔文件的路徑,準備需要歸檔的數據
2⃣️:寫一個可變的NSMutableData 通過歸檔的類,讓NSData準備寫入到文件中
3⃣️:開始歸檔(編碼 Coding)把Data數據寫到文件中
➕:歸檔實例
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *anotherPath =[[path lastObject] stringByAppendingPathComponent:@“userList1.archiver"];
// 上兩句是文件路徑
1. 歸檔文件的路徑 準備需要歸檔的數據
NSArray *ray = @[@"123",@"23",@"34"];
NSDictionary *dic = @{@"1":@"A",@"2":@"B",@"3":@"C"};
2.寫一個可變的NSMutableData 通過歸檔的類 讓NSData準備寫入到文件中
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver =[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];// 準備寫入文件
3.開始歸檔(編碼) 編碼的key 和 解碼的時候的key 一定要統一
[archiver encodeObject:ray forKey:@"ray"];
4.結束歸檔 編碼 將Data數據寫入到文件中
[archiver finishEncoding];
BOOL success = [data writeToFile:anotherPath atomically:YES];//在寫入數據時 如果出現意外情況(用戶突然強制退出了應用程序,斷電....)會把寫入到臨時文件的內容清除掉 不再寫入到目標文件,如果atomically是NO,不管數據是否完整都會直接寫入到目標文件
if (success) {
[self showAlertWithMessage:@"歸檔成功"];
}
2.解歸檔步驟:
1⃣️:文件路徑
2⃣️:讀取文件裏面的Data數據
3⃣️:通過解歸檔對象
4⃣️:解歸檔數據
➕:解歸檔實例
1.文件路徑
/* NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject] stringByAppendingPathComponent:@"userList1.archiver"];
// 2.讀取文件裏面的Data數據
NSData *data = [NSData dataWithContentsOfFile:paths];
// 3.通過解歸檔對象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
// 4.解歸檔 解碼
NSArray *arr =[unarchiver decodeObjectForKey:@"ray"];
NSDictionary *dii = [unarchiver decodeObjectForKey:@"dic"];
NSLog(@"%@ %@",arr,dii);
// [unarchiver decodeObjectForKey:@"ray"];
歸檔自定義類的對象
條件:
1⃣️:遵守歸檔協議NSCoding
2⃣️:實現歸檔協議裏面的方法
3⃣️:逐一對自定義類裏面的屬性進行編碼和解碼,這個對象就具備了歸檔和解歸檔的功能了
4⃣️:開始歸檔 解歸檔
➕:歸檔實例
// 要歸檔userModel裏面的 name、age屬性內容
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject]stringByAppendingPathComponent:@"userModel.archiver"];
userModel *model = [[userModel alloc]init];
model.name = @"葫蘆娃";
model.age = 18;
BOOL success = [NSKeyedArchiver archiveRootObject:model toFile:paths];
if (success) {
[self showAlertWithMessage:@"歸檔成功"];
NSLog(@"%@,%ld",model.name,(long)model.age);
}
// 對已經歸檔的userModel裏面的name、age屬性解歸檔
➕:解歸檔實例
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject]stringByAppendingPathComponent:@"userModel.archiver"];
userModel *p = [NSKeyedUnarchiver unarchiveObjectWithFile:paths];
NSLog(@"%@,%ld",p.name,(long)p.age);