plist:plist是iOS中數(shù)據(jù)存儲的方式之一磺芭、一般對
Foundation
框架的NSArray
等進(jìn)行存儲的技術(shù)屬性列表是一種XML格式的文件,拓展名為plist
如果對象是NSString、NSDictionary、NSArray诫隅、NSData损同、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中
NSDictionary的plist存儲過程
- 將一個(gè)NSDictionary對象歸檔到一個(gè)plist屬性列表中
// 將數(shù)據(jù)封裝成字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"母雞" forKey:@"name"];
[dict setObject:@"15013141314" forKey:@"phone"];
[dict setObject:@"27" forKey:@"age"];
// 將字典持久化到Documents/stu.plist文件中
[dict writeToFile:path atomically:YES];
- 讀取屬性列表慈鸠,恢復(fù)NSDictionary對象
// 讀取Documents/stu.plist的內(nèi)容蓝谨,實(shí)例化NSDictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"name:%@", [dict objectForKey:@"name"]);
NSLog(@"phone:%@", [dict objectForKey:@"phone"]);
NSLog(@"age:%@", [dict objectForKey:@"age"]);
加載Main Bundle中的plist(自己加入工程的)
//獲得Plist文件的全路徑
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"shops" ofType:@"plist"];
//加載plist文件
[NSArray arrayWithContentsOfFile:path];
```