在作iOS开发时,咱们应该常常会用到plist文件,那么什么是plist文件呢?它全名是Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,所以一般被称为 plist文件。文件是xml格式的。plist文件一般用于储存用户设置,也能够用于存储捆绑的信息,例如工程中的Info.plist文件。相似于android中的SharedPreferences。android
- (void)viewDidLoad { [super viewDidLoad]; //我这里plist文件名为shop.plist //利用mainBundle来关联主资源包 NSBundle *bundle = [NSBundle mainBundle]; //获取plist文件所在的全路径 NSFile *file = [bundle pathForResource:@"shop" ofType:@"plist"]; //也能够是这样[bundle pathForResource:@"shop.plist" ofType:nil] //根据全路径获得一个数组对象,里面就是plist文件中的内容 NSArray *dictArray = [NSArray arrayWithContentsOfFile:file]; //打印数据 NSLog(@"%@", dictArray); }
三、写入数据到plist文件(先保存,后面再补充吧)数组
//plist的存储 NSArray *arr = @[@"111", @"222"]; //获取Caches文件夹 NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; //拼接文件名 NSString *filePath = [cachePath stringByAppendingPathComponent:@"demo.plist"]; [arr writeToFile:filePath atomically:YES];