//這個方法獲取出的結果是一個數組.由於有能夠搜索到多個路徑.
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 //底層就是封閉了一個字典,利用字典的方式生成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);
歸檔的存儲:NSKeyedArchiveride
//獲取沙盒的路徑 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];
讀取:atom
//獲取沙盒的路徑
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協議才行
//當解析一個文件時就會調用.
-(instancetype)initWithCoder:(NSCoder *)decoder{ if (self = [super init]) { self.name = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"]; } return self; }
//當對象寫入文件時候調用
-(void)encodeWithCoder:(NSCoder *)encoder{ [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeInt:self.age forKey:@"age"]; }