iOS開發中數據存在五種存儲方式之三:
1.plist(XML屬性列表歸檔)
2.偏好設置
3.NSKeydeArchiver歸檔(存儲自定義對象)
一、plist(XML屬性列表歸檔)只能存取對象類文件
第一種方式:(四個文件夾都可以取出路徑)
1 //獲取沙盒路徑 2 NSString *home = NSHomeDirectory(); 3 //獲取documents的路徑兩種方式(其它三個文件夾也能獲取) 4 NSString *path = [home stringByAppendingString:@"/documents"]; 5 NSString *path1 = [home stringByAppendingPathComponent:@"library/cache"];
第二種方式:(僅有documents和cache能取出路徑)(獲取路徑的時候必須寫成YES,否則無法成功寫入)
1 //獲取cache路徑,NO表示文件路徑前面用“~”表示 2 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject]; 3 NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]; 4 //拼接路徑 5 NSString *path = [cache stringByAppendingPathComponent:@"abc.plist"]; 6 NSString *path1 = [doucument stringByAppendingPathComponent:@"/ce.plist"]; 7 //把文件寫入文件夾 8 [arr writeToFile:path atomically:YES]; 9 [arr writeToFile:path1 atomically:YES];
讀取文件:
1 //讀取文件 2 NSArray *arr1 = [NSArray arrayWithContentsOfFile:path]; 3 NSLog(@"%@",arr1);
二、偏好設置(保存用戶名、密碼、字體大小、是否自動登錄等等基本設置)自動保存到沙盒的Preferences文件夾中
1 //獲取對象 2 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; 3 //存儲數據 4 [ud setObject:@"abc" forKey:@"這是什么"]; 5 [ud setObject:@"cde" forKey:@"俺是"]; 6 //讀取數據 7 NSString *str = [ud objectForKey:@"俺是"]; 8 NSString *str1 = [ud objectForKey:@"這是什么"]; 9 NSLog(@"%@---%@",str,str1);
注意:偏好設置存儲時候,保存的時間不確定,可能是將來的某個時間存儲到文件夾中,所以可以馬上同步存儲:
[ud synchronize];
三、NSKeyedArchive的歸檔(存儲自定義對象)
在viewController.m中:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 ZWPerson *p = [[ZWPerson alloc] init]; 5 p.name = @"mc"; 6 p.age = 18; 7 p.height = 1.88; 8 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject]; 9 NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]; 10 //拼接路徑 11 NSString *path = [cache stringByAppendingPathComponent:@"abc.doc"]; 12 NSString *path1 = [doucument stringByAppendingPathComponent:@"/ce.doc"]; 13 //存儲數據 14 BOOL ar = [NSKeyedArchiver archiveRootObject:p toFile:path]; 15 BOOL ar1 = [NSKeyedArchiver archiveRootObject:p toFile:path1]; 16 NSLog(@"%d---%d",ar,ar1); 17 //讀取數據 18 ZWPerson *p1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 19 ZWPerson *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path1]; 20 NSLog(@"%zd---%@",p1.age,p2.name); 21 }
新建ZWPerson類(繼承自ViewController)
在ZWPerson.h中:
1 #import "ViewController.h" 2 @interface ZWPerson : ViewController 3 /** 名字 */ 4 @property (strong, nonatomic)NSString *name; 5 /** 年齡 */ 6 @property (assign, nonatomic)NSInteger age; 7 /** 身高 */ 8 @property (assign, nonatomic)double height; 9 @end
在ZWPerson.h中:
1 #import "ZWPerson.h" 2 @interface ZWPerson () 3 @end 4 @implementation ZWPerson 5 //存儲的時候調用這個方法 6 - (void)encodeWithCoder:(NSCoder *)aCoder 7 { 8 //存儲對象的屬性(根據實際需要存儲數據) 9 [aCoder encodeObject:self.name forKey:@"name"]; 10 [aCoder encodeInteger:self.age forKey:@"age"]; 11 [aCoder encodeDouble:self.height forKey:@"height"]; 12 } 13 //讀取的時候調用 14 - (instancetype)initWithCoder:(NSCoder *)aDecoder 15 { 16 if (self = [super initWithCoder:aDecoder]) {. 17 //獲取對象的屬性(根據實際需要獲取數據) 18 self.name = [aDecoder decodeObjectForKey:@"name"]; 19 self.age = [aDecoder decodeIntegerForKey:@"age"]; 20 self.height = [aDecoder decodeDoubleForKey:@"height"]; 21 } 22 return self; 23 } 24 @end
如果是繼承自NSObject:則只需要更改initWithCoder中的if(self = super init)即可
注意:1、遵守NSCoding協議,並實現該協議中的兩個方法。
2、如果是繼承,則子類一定要重寫那兩個方法。因為person的子類在存取的時候,會去子類中去找調用的方法,沒找到那么它就去父類中找,所以最后保存和讀取的時候新增加的屬性會被忽略。需要先調用父類的方法,先初始化父類的,再初始化子類的。
3、保存數據的文件的后綴名可以隨意命名。
4、通過plist保存的數據是直接顯示的,不安全。通過歸檔方法保存的數據在文件中打開是亂碼的,更安全。