plist
文件, 全名是 Property List
因為文件以 .plist
結(jié)尾,所以通常叫做 plist
文件。最外層的 type
只有 字典和數(shù)組兩種類型。
支持的數(shù)據(jù)類型
NSData NSString NSNumber NSDate NSArray NSDictionary
全部都是不可變的
創(chuàng)建方式
1.手動創(chuàng)建
barneyzhaoooo
2.代碼創(chuàng)建
NSString *plistPath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"test.plist"];
數(shù)據(jù)寫入
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"ceshi"
forKey:@"name"];
[dic setObject:@"12"
forKey:@"age"];
[dic setObject:@"man"
forKey:@"sex"];
[dic writeToFile:plistPath
atomically:YES];
讀取
1.手動創(chuàng)建的讀取方式
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"download"
ofType:@"plist"];
NSMutableArray *dataArr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
2.代碼創(chuàng)建的讀取方式,依然通過路徑獲取
NSString *plistPath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"test.plist"];
// NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(@"%@",data);
打印結(jié)果:
2017-06-01 11:32:28.126 AYDataBaseDemo[1989:518382] {
age = 12;
name = zhangsan1111;
sex = man;
}
root
層是什么類型,就用什么類型來解析。
刪除 plist 文件
NSFileManager *fileMger = [NSFileManager defaultManager];
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"test.plist"];
NSLog(@"%@",filePath);
if ([fileMger fileExistsAtPath:filePath]) {// 如果存在就刪除
NSError *err;
[fileMger removeItemAtPath:filePath
error:&err];
}
修改,刪除,覆蓋
新建一個數(shù)組寫入之前的路徑,再次打印發(fā)現(xiàn)數(shù)據(jù)已經(jīng)被覆蓋。
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:@"1"];
[arr addObject:@"2"];
[arr addObject:@"3"];
[arr writeToFile:plistPath
atomically:YES];
NSMutableArray *arrRead = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSLog(@"%@",arrRead);
打印結(jié)果:可以發(fā)現(xiàn)數(shù)據(jù)已經(jīng)變成了 1 2 3
2017-06-01 14:23:17.545 AYDataBaseDemo[2595:818062] {
age = 12;
name = ceshi;
sex = man;
}
2017-06-01 14:23:17.546 AYDataBaseDemo[2595:818062] (
1,
2,
3
)
所以 plist
文件的修改和刪除單條數(shù)據(jù)其實是先 讀取 copy
原數(shù)據(jù),修改后整體重新覆蓋寫入,并不是之前期待的類似關(guān)系型數(shù)據(jù)庫的操作,但是存一些簡單的用戶信息,基本設(shè)置還是很適合的,畢竟殺雞焉用宰牛刀~
判斷路徑下指定文件是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
NSLog(@"exist!");
}
NSUserDefaults 系統(tǒng)封裝了一個單例的類,方便使用
系統(tǒng)提供了單例的初始化方式
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
簡單寫入數(shù)據(jù),強(qiáng)制同步 synchronize 并非必要
[defaults setObject:@"barney"
forKey:@"firstName"];
[defaults setInteger:18
forKey:@"Age"];
[defaults synchronize];
讀取
NSString *firstName = [defaults objectForKey:@"firstName"];
NSInteger age = [defaults integerForKey:@"Age"];
NSLog(@"firstName = %@,age = %ld",firstName,(long)age);
還可以利用歸檔存儲自定義 model
在 model
類實現(xiàn) NSCoding
協(xié)議
@interface OneModel : NSObject <NSCoding>
@property (nonatomic, copy) NSString *addid;
@property (nonatomic, copy) NSString *age;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *salary;
@end
@implementation OneModel
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.addid
forKey:@"addid"];
[aCoder encodeObject:self.age
forKey:@"age"];
[aCoder encodeObject:self.name
forKey:@"name"];
[aCoder encodeObject:self.salary
forKey:@"salary"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.addid = [aDecoder decodeObjectForKey:@"addid"];
self.age = [aDecoder decodeObjectForKey:@"age"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.salary = [aDecoder decodeObjectForKey:@"salary"];
}
return self;
}
@end
model 類聲明并且賦值
OneModel *model = [[OneModel alloc] init];
[model setAddid:@"1"];
[model setName:@"barney"];
[model setAge:@"18"];
[model setSalary:@"1000"];
轉(zhuǎn)換 model
類為 data
類型
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
[defaults setObject:data forKey:@"model"];
獲取 data
還原回 model
NSData *getData = [defaults objectForKey:@"model"];
OneModel *getModel = [NSKeyedUnarchiver unarchiveObjectWithData:getData];
NSLog(@"addid = %@ name = %@ age = %@ salary = %@",getModel.addid,getModel.name,getModel.age,getModel.salary);
成功打印
2017-06-01 15:49:52.534 AYDataBaseDemo[3040:1210973] addid = 1 name = barney age = 18 salary = 1000
這種數(shù)據(jù)持久化的方式用在小小的項目上還好,稍大稍復(fù)雜的項目通常會使用 SQLite 。或者是 CoreData。