XML屬性列表-plist
一、應用沙盒
每個iOS應用都有⾃己的應⽤沙盒(應用沙盒就是文件系統目錄),與其他文件系統隔離。應⽤必須待在⾃己的沙盒里,其他應用不能訪問該沙盒(提示:在IOS8中已經開放訪問)
應⽤沙盒的文件系統⽬錄,如下圖所示(假設應用的名稱叫Layer)
模擬器應⽤用沙盒的根路徑在: (apple是⽤用戶名, 7.0是模擬器版本) /Users/apple/Library/Application Support/iPhone Simulator/7.0/Applications
二、應用沙盒結構分析
應⽤程序包:(上圖中的Layer)包含了所有的資源文件和可執行文件
Documents:保存應⽤運行時生成的需要持久化的數據,iTunes同步設備時會備份該目錄。例如,游戲應用可將游戲存檔保存在該目錄
tmp:保存應⽤運行時所需的臨時數據,使⽤完畢后再將相應的文件從該目錄刪除。應用沒有運行時,系統也可能會清除該目錄下的文件。iTunes同步設備時 不會備份該目錄
Library/Caches:保存應用運行時⽣成的需要持久化的數據,iTunes同步設備時不會備份該目錄。⼀一般存儲體積大、不需要備份的非重要數據
Library/Preference:保存應用的所有偏好設置,iOS的Settings(設置) 應⽤會在該⺫錄中查找應⽤的設置信息。iTunes同步設備時會備份該目錄
三、應用沙盒常見的獲取方式
沙盒根目錄:NSString *home = NSHomeDirectory();
Documents:(2種⽅方式)
利用沙盒根目錄拼接”Documents”字符串
NSString *documents = [array objectAtIndex:0];
tmp:NSString *tmp = NSTemporaryDirectory();
利用沙盒根目錄拼接”Caches”字符串
利⽤NSSearchPathForDirectoriesInDomains函數(將函數的第2個參數改 為:NSCachesDirectory即可)
Library/Preference:通過NSUserDefaults類存取該目錄下的設置信息
相應的代碼:
@interface NJViewController ()
- (IBAction)saveDataBtnClick:(id)sender;
- (IBAction)readDataBtnClick:(id)sender;
@end
屬性列表是一種XML格式的文件,拓展名為plist
如果對象是NSString、NSDictionary、NSArray、NSData、 NSNumber等類型,就可以使用writeToFile:atomically:⽅法 直接將對象寫到屬性列表文件中
NSKeydeArchiver歸檔
一、簡單說明
在使用plist進行數據存儲和讀取,只適用于系統自帶的一些常用類型才能用,且必須先獲取路徑相對麻煩;
偏好設置(將所有的東西都保存在同一個文件夾下面,且主要用于存儲應用的設置信息)
歸檔:因為前兩者都有一個致命的缺陷,只能存儲常用的類型。歸檔可以實現把自定義的對象存放在文件中。
二、代碼示例
1.文件結構
#import "YYViewController.h"
#import "YYPerson.h"
@interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)saveBtnOnclick:(id)sender {
//1.創建對象
YYPerson *p=[[YYPerson alloc]init];
p.name=@"文頂頂";
p.age=23;
p.height=1.7;
//2.獲取文件路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
//3.將自定義的對象保存到文件中
[NSKeyedArchiver archiveRootObject:p toFile:path];
}
- (IBAction)readBtnOnclick:(id)sender {
//1.獲取文件路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
//2.從文件中讀取對象
YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
}
@end
YYPerson.h文件
#import <Foundation/Foundation.h>
// 如果想將一個自定義對象保存到文件中必須實現NSCoding協議
@interface YYPerson : NSObject<NSCoding>
//姓名
@property(nonatomic,copy)NSString *name;
//年齡
@property(nonatomic,assign)int age;
//身高
@property(nonatomic,assign)double height;
@end
#import "YYPerson.h"
@implementation YYPerson
// 當將一個自定義對象保存到文件的時候就會調用該方法
// 在該方法中說明如何存儲自定義對象的屬性
// 也就說在該方法中說清楚存儲自定義對象的哪些屬性
-(void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"調用了encodeWithCoder:方法");
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeDouble:self.height forKey:@"height"];
}
// 當從文件中讀取一個對象的時候就會調用該方法
// 在該方法中說明如何讀取保存在文件中的對象
// 也就是說在該方法中說清楚怎么讀取文件中的對象
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"調用了initWithCoder:方法");
//注意:在構造方法中需要先初始化父類的方法
if (self=[super init]) {
self.name=[aDecoder decodeObjectForKey:@"name"];
self.age=[aDecoder decodeIntegerForKey:@"age"];
self.height=[aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
點擊保存按鈕和讀取按鈕,成功打印結果如下:
關于不實現兩個協議方法的錯誤提示:
-(void)encodeWithCoder:(NSCoder *)aCoder方法:
-(id)initWithCoder:(NSCoder *)aDecoder方法:
三、繼承類中的使用
新建一個學生類,讓這個類繼承自Preson這個類,增加一個體重的屬性。
YYstudent.h文件
#import "YYPerson.h"
@interface YYstudent : YYPerson
//增加一個體重屬性
@property(nonatomic,assign) double weight;
@end
#import "YYstudent.h"
@implementation YYstudent
//在子類中重寫這兩個方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
NSLog(@"調用了YYStudent encodeWithCoder");
[aCoder encodeFloat:self.weight forKey:@"weight"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"調用了YYstudent initWithCoder");
self.weight = [aDecoder decodeFloatForKey:@"weight"];
}
return self;
}
@end
#import "YYViewController.h"
#import "YYPerson.h"
#import "YYstudent.h"
@interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)saveBtnOnclick:(id)sender {
//1.創建對象
// YYPerson *p=[[YYPerson alloc]init];
// p.name=@"文頂頂";
// p.age=23;
// p.height=1.7;
YYstudent *s=[[YYstudent alloc]init];
s.name=@"wendingding";
s.age=23;
s.height=1.7;
s.weight=62;
//2.獲取文件路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
//3.將自定義的對象保存到文件中
// [NSKeyedArchiver archiveRootObject:p toFile:path];
[NSKeyedArchiver archiveRootObject:s toFile:path];
}
- (IBAction)readBtnOnclick:(id)sender {
//1.獲取文件路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
//2.從文件中讀取對象
// YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f,%f",s.name,s.age,s.height,s.weight);
}
@end
四、重要說明
1.保存數據過程:
4.如果是繼承,則子類一定要重寫那兩個方法。因為person的子類在存取的時候,會去子類中去找調用的方法,沒找到那么它就去父類中找,所以最后保存和讀取的時候新增加的屬性會被忽略。需要先調用父類的方法,先初始化父類的,再初始化子類的。
5.保存數據的文件的后綴名可以隨意命名。
6.通過plist保存的數據是直接顯示的,不安全。通過歸檔方法保存的數據在文件中打開是亂碼的,更安全。
新聞熱點
疑難解答