標籤:沙箱
什麼是資料持久化?資料的永久儲存
為什麼要坐資料持久化:儲存在記憶體中的資料,程式關閉,記憶體釋放,資料丟失,這種資料是臨時的
資料初九化的本質:資料儲存成檔案,儲存到程式的沙河中
1.沙箱機制
每個應用程式位於檔案系統的嚴格限制部分
每個應用程式只能在為該程式建立的檔案系統中讀取檔案
每個應用程式在IOS系統內都放在了統一的檔案夾目錄下
沙箱的本質就是一個檔案夾,名字是隨機分配的.
2.沙箱路徑的位置
1.通過Finder尋找程式沙箱相對的路徑
通過代碼尋找程式沙箱相對路徑
NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",DocumentPath);
2.沙箱構成
Document 儲存使用者資料,需要備份的資訊
Library/Caches 儲存快取檔案,程式專用的支援檔案
Library/Preferences 儲存應用程式的喜好設定檔案
.app 程式包(IOS8時,app不儲存在沙箱中,有單獨的檔案夾儲存所有程式的app包)
tmp 儲存臨時檔案,比如:下載的zip包,解壓後的再刪除
3.擷取沙箱目錄路徑的方法
NSHomeDirectory-------------------->沙箱主路徑
NSDocumentDirectory--------------->Document檔案夾
NSLibraryDirectory------------------->Library檔案夾
NSCachesDirectory------------------>Caches檔案夾
NSTemporaryDirectory--------------->tem檔案夾
代碼
<span style="font-family:SimHei;font-size:18px;">//1.home主目錄裡面有:Documents,Library,tmp和一個應用程式 NSLog(@"Home:%@",NSHomeDirectory()); //2.DocumentsPath路徑 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"DocumentsPath:%@",DocumentsPath); //3.Libray NSString *librayPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",librayPath); //4.temp NSLog(@"temp:%@",NSTemporaryDirectory()); //5.cachesPath NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"cachesPath:%@",cachesPath); //6.user NSString *user = NSUserName(); NSLog(@"user:%@",user);</span>
------------------------------------------------>>>>簡單檔案寫入
<span style="font-family:SimHei;font-size:18px;">//nsstring寫入 //1.寫入的路徑 NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",DocumentPath); //2.拼接檔案路徑 NSString *filePath = [DocumentPath stringByAppendingString:@"/myText.txt"]; //3.準備寫入的內容 NSString *content = @"Hello World"; [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; //4.讀取 NSString *readString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"redstring : %@",readString); //NSArray //1.擷取documents路徑 //2.拼接檔案路徑 NSString *arrayFile = [DocumentPath stringByAppendingString:@"/array.plist"]; NSLog(@"arrayPath = %@",arrayFile); //3.準備內容 NSArray * contentArray = @[@"1",@"2",@"3",@"4",@"5",]; //4.寫入 [contentArray writeToFile:arrayFile atomically:YES]; //5.讀取 NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayFile]; NSLog(@"readArray : %@",readArray); //dictinary //1.拼接 NSString *dictFile = [DocumentPath stringByAppendingString:@"/dict.plist"]; //2.準備內容 NSDictionary *dictcontent = @{@"1":@"a",@"2":@"b",@"3":@"c"}; NSLog(@"%@",dictFile); //3.寫入 [dictcontent writeToFile:dictFile atomically:YES]; //4.讀取字典 NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:dictFile]; NSLog(@"dict : %@",readDict);</span>-------------------------------->>>NSFileManager
NSFileManager,檔案管理,使用detaultManager,建立單利對象
可以建立檔案夾
可以建立,移動,複製,刪除檔案,
可以判斷檔案是否存在
<span style="font-family:SimHei;font-size:18px;"> //NSFileManager //建立檔案夾 //在Documents中建立一個檔案夾 NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //在Documents中建立一個檔案夾命名為"個人收藏" NSString *path = [documentsPath stringByAppendingString:@"/個人收藏"]; //建立檔案管理(單利),並建立檔案夾 [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"DocumentsPath%@",documentsPath); //修改檔案夾 NSString *newPath = [documentsPath stringByAppendingString:@"/島國文化"]; [[NSFileManager defaultManager]moveItemAtPath:path toPath:newPath error:nil]; //刪除 [[NSFileManager defaultManager]removeItemAtPath:documentsPath error:nil]; //判斷某個檔案是否存在 //傳回值是BOOL,YES存在,NO不存在 [[NSFileManager defaultManager]fileExistsAtPath:newPath];</span>
--------------------------------------------------->>>>複雜物件寫入檔案(歸檔/反歸檔)
1.什麼是複雜物件
1.在foundation架構內不存在的資料類
2.無法在程式內通過writeToFile類型的方法寫入到檔案內
3.複雜物件至少包含一個執行個體對象
複雜物件無法通過writeToFile:方法進行資料持久化,只能通過將複雜物件轉換為NSData,通過writeToFile進行資料持久化
將複雜的對象轉化為NSData,通過歸檔;將NSData轉換為複雜物件,通過反歸檔
複雜物件寫入檔案要遵循NSCoding協議
有兩個方法,代碼如下:
<span style="font-family:SimHei;font-size:18px;">//進行歸檔時調用(系統調用)-(void)encodeWithCoder:(NSCoder *)aCoder{ //對屬性進行編碼 [aCoder encodeObject:self.name forKey:kName]; [aCoder encodeObject:self.age forKey:kAge];}//進行反歸檔編碼時-(id)initWithCoder:(NSCoder *)aDecoder{ self = [super init]; //反編碼 if (self) { self.name = [aDecoder decodeObjectForKey:kName]; } return self;}</span>建立一個Person類
歸檔/反歸檔代碼如下:
<span style="font-family:SimHei;font-size:18px;">//歸檔 反歸檔 //建立 Person類執行個體對象 Person *person1 = [[Person alloc] init]; person1.name = @"劉傑"; person1.age = @"39"; Person *person2 = [[Person alloc] init]; person2.name = @"李士傑"; person2.age = @"18"; //歸檔使用的NSData NSMutableData *Person1Data = [NSMutableData data]; //建立歸檔工具 NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:Person1Data]; //進行歸檔 [archiver encodeObject:person1 forKey:kPerson1]; [archiver encodeObject:person2 forKey:kPerson2]; //完成轉換 [archiver finishEncoding]; //找到路徑 NSString *docunment = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接檔案路徑 NSString *personPath = [docunment stringByAppendingString:@"/劉傑.xxoo"]; //寫入文檔 [Person1Data writeToFile:personPath atomically:YES]; NSLog(@"%@",docunment); //反歸檔 //通過檔案路徑,擷取data資料 NSData * unData = [NSData dataWithContentsOfFile:personPath]; //反歸檔工具 NSKeyedUnarchiver *unAechiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unData]; //反歸檔 Person *p1 = [unAechiver decodeObjectForKey:kPerson1]; Person *p2 = [unAechiver decodeObjectForKey:kPerson2]; //結束反歸檔 [unAechiver finishDecoding]; NSLog(@"name:%@",p1.name); NSLog(@"name :%@",p2.name);</span>
單個歸檔.反歸檔
<span style="font-family:SimHei;font-size:18px;"> //擷取Documents路徑 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接檔案路徑 NSString *filePath = [DocumentsPath stringByAppendingString:@"/PersonArray.plist"]; //執行個體一個對象 Person *p1 = [[Person alloc] init]; p1.name = @"別鬧了"; p1.age = @"1"; //歸檔 [NSKeyedArchiver archiveRootObject:p1 toFile:filePath]; //反歸檔 Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"name: %@",p2.name);</span>
多個歸檔/反歸檔
<span style="font-family:SimHei;font-size:18px;"> //擷取Documents路徑 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接檔案路徑 NSString *filePath = [DocumentsPath stringByAppendingString:@"/PersonArray.plist"]; //執行個體一個對象 Person *pn1 = [[Person alloc] init]; pn1.name = @"TOM"; pn1.age = @"12"; Person *pn2 = [[Person alloc] init]; pn2.name = @"KIM"; pn2.age = @"18"; NSArray *array = @[pn1,pn2]; //歸檔 [NSKeyedArchiver archiveRootObject:array toFile:filePath]; //反歸檔 NSArray *a = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"%@,%@",[a[0] name],[a[1] name]);</span>
沙箱機制:
簡單對象寫入檔案,只能是NSString,NSArray,NSDictionary,NSData
複雜的對象寫入檔案,遵守NSCoding協議,實現代理方法
IOS 初級資料持久化-沙箱機制