plist存儲
- (IBAction)save:(id)sender {
//數據存儲是保存在手機里面的
//plist文件存儲一般都是存取字典和數組,直接寫成plist文件,把它存到應用沙盒當中.
//只有在ios當中才有plist存儲,它是ios特有的存儲方式.
//獲取沙盒根根路徑,每一個應用在手機當中都有一個文件夾,這個方法就是獲取當前應用在手機里安裝的文件.
//NSString *homeDir = NSHomeDirectory();
//NSLog(@"homeDir = %@",homeDir);
//在某個范圍內搜索文件夾的路徑.
//directory:獲取哪個文件夾
//domainMask:在哪個路徑下搜索
//expandTilde:是否展開路徑.
//這個方法獲取出的結果是一個數組.因為有可以搜索到多個路徑.
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//在這里,我們指定搜索的是Cache目錄,所以結果只有一個,取出Cache目錄
NSString *cachePath = array[0];
NSLog(@"%@",cachePath);
//拼接文件路徑
NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];
//想要把這個字典存儲為plist文件.
//直接把字典寫入到沙盒當中
//用字典寫, plist文件當中保存的是字典.
NSDictionary *dict = @{@"age" : @18,@"name" : @"gaowei"};
//獲取沙盒路徑
//ToFile:要寫入的沙盒路徑
[dict writeToFile:filePathName atomically:YES];
//用數組寫,plist文件當中保存的類型是數組.
NSArray *dataArray = @[@56,@"asdfa"];
//獲取沙盒路徑
//ToFile:要寫入的沙盒路徑
[dataArray writeToFile:filePathName atomically:YES];
}
- (IBAction)reader:(id)sender {
//這個方法獲取出的結果是一個數組.因為有可以搜索到多個路徑.
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//在這里,我們指定搜索的是Cache目錄,所以結果只有一個,取出Cache目錄
NSString *cachePath = array[0];
NSLog(@"%@",cachePath);
//拼接文件路徑
NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];
//從文件當中讀取字典, 保存的plist文件就是一個字典,這里直接填寫plist文件所存的路徑
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePathName];
NSLog(@"%@",dict);
//如果保存的是一個數組.那就通過數組從文件當中加載.
NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
NSLog(@"%@",dataArray);
}
偏好設置
- (IBAction)save:(id)sender {
//偏好設置NSUserDefaults
//底層就是封閉了一個字典,利用字典的方式生成plist文件
//好處:不需要關心文件名(它會自動生成)快速進行鍵值對存儲.
NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];
[defautls setObject:@"gaowei" forKey:@"name"];
[defautls setBool:YES forKey:@"isBool"];
[defautls setInteger:5 forKey:@"num"];
//同步,立即寫入文件.
[defautls synchronize];
}
- (IBAction)reader:(id)sender {
//存是用什么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);
}
歸檔
- (IBAction)save:(id)sender {
//歸檔一般都是保存自定義對象的時候,使用歸檔.因為plist文件不能夠保存自定義對象.
//如果一個字典當中保存有自定義對象,如果把這個字典寫入到文件當中,它是不會生成plist文件的.
Persion *persion = [[Persion alloc] init];
persion.name = @"gaowei";
persion.age = 18;
//獲取沙盒臨時目錄
NSString *tempPath = NSTemporaryDirectory();
NSString *filePath = [tempPath stringByAppendingPathComponent:@"persion.data"];
NSLog(@"%@",filePath);
//archiveRootObject這個方法底層會去調用保存對象的encodeWithCoder方法,去詢問要保存這個對象的哪些屬性.
//所以要實現encodeWithCoder方法, 告訴要保存這個對象的哪些屬性.
[NSKeyedArchiver archiveRootObject:persion toFile:filePath];
}
- (IBAction)reader:(id)sender {
//獲取沙盒臨時目錄
NSString *tempPath = NSTemporaryDirectory();
NSString *filePath = [tempPath stringByAppendingPathComponent:@"persion.data"];
//NSKeyedUnarchiver會調用initWithCoder這個方法,來讓你告訴它去獲取這個對象的哪些屬性.
//所以我們在保存的對象當中實現initWithCoder方法.
Persion *persion = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"name=%@---age=%d",persion.name,persion.age);
}