標籤:
緩衝的儲存:NSSearchPathForDirectoriesInDomains儲存:
//這個方法擷取出的結果是一個數組.因為有可以搜尋到多個路徑. NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); //在這裡,我們指定搜尋的是Cache目錄,所以結果只有一個,取出Cache目錄 NSString *cachePath = array[0]; //拼接檔案路徑,stringByAppendingPathComponent會在cachePath路徑後面加上/再拼接 NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"]; //1.用字典寫, plist檔案當中儲存的是字典. NSDictionary *dict = @{@"age" : @18,@"name" : @"gaowei"}; //ToFile:要寫入的沙箱路徑 [dict writeToFile:filePathName atomically:YES]; //2.用數組寫,plist檔案當中儲存的類型是數組 NSArray *dataArray = @[@56,@"asdfa"]; //ToFile:要寫入的沙箱路徑 [dataArray writeToFile:filePathName atomically:YES];
讀取:
//這個方法擷取出的結果是一個數組.因為有可以搜尋到多個路徑. NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); //在這裡,我們指定搜尋的是Cache目錄,所以結果只有一個,取出Cache目錄 NSString *cachePath = array[0]; //拼接檔案路徑 NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"]; //從檔案當中讀取字典, 儲存的plist檔案就是一個字典,這裡直接填寫plist檔案所存的路徑 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePathName]; //如果儲存的是一個數組.那就通過數組從檔案當中載入. NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
喜好設定的儲存:NSUserDefaults儲存:
//喜好設定NSUserDefaults //底層就是封閉了一個字典,利用字典的方式產生plist檔案 //好處:不需要關心檔案名稱(它會自動產生)快速進行索引值對儲存. NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults]; [defautls setObject:@"gaowei" forKey:@"name"]; [defautls setBool:YES forKey:@"isBool"]; [defautls setInteger:5 forKey:@"num"]; //同步,立即寫入檔案. [defautls synchronize];
讀取:
//存是用什麼key存的, 讀的時候就要用什麼key值取 //存的時候使用的什麼類型,取的時候也要用什麼類型. NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"]; BOOL isBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"isBool"]; NSInteger num = [[NSUserDefaults standardUserDefaults] integerForKey:@"num"]; NSLog(@"name =%@-isBool=%d-num=%ld",str,isBool,num);
歸檔的儲存:NSKeyedArchiver
與pist儲存,喜好設定儲存不同的是,歸檔可用於儲存自訂的類,而那倆者只能儲存系統的類的資料
//擷取沙箱的路徑 person.data 隨便寫 NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *filePathName = [path stringByAppendingPathComponent:@"person.data"];// Person *per = [[Person alloc] init];// per.name = @"xmg";// per.age = 10; Student *stu = [[Student alloc] init]; stu.name = @"gxq"; stu.age = 10; stu.height = 150; Dog *dog = [[Dog alloc] init]; dog.name = @"wangcai"; stu.dog = dog; //歸檔 //archiveRootObject執行這個方法時, 底層會調用要存的對象的encodeWithCoder方法, //調用encodeWithCoder目的就是想詢問下要存對象的哪些屬性.怎麼存. [NSKeyedArchiver archiveRootObject:stu toFile:filePathName];
讀取:
//擷取沙箱的路徑 NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *filePathName = [path stringByAppendingPathComponent:@"person.data"]; //解檔 //unarchiveObjectWithFile執行這個方法時,底層會調用要存的對象的initWithCoder方法, //調用initWithCoder目的就是想詢問下要取對象的哪些屬性. Student *per = [NSKeyedUnarchiver unarchiveObjectWithFile:filePathName];
//注意點: 不管是自訂的類對象"per”,繼承的類對象”stu”,包含的類對象”dog”,只要歸檔的時候,都要實現encodeWithCoder,解檔就要實現initWithCoder//必須得要遵守NSCoding協議才行-(void)encodeWithCoder:(NSCoder *)encoder{ [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeInt:self.age forKey:@"age"];}//當解析一個檔案時就會調用.-(instancetype)initWithCoder:(NSCoder *)decoder{ if (self = [super init]) { self.name = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"]; } return self;}
iOS開發-資料存放區