plist文件简单使用 - iOS

// 新建文件创建plist文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"banks" ofType:@"plist"];
//存储类型要对应 不然取出为null
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSLog(@"plist文件%@", data);
NSDictionary *dic = data[1];
NSString * bankcode = dic[@"bankcode"];
NSString * bankeng = dic[@"bankeng"];
NSString * bankname = dic[@"bankname"];
NSString * img = dic[@"img"];

NSLog(@"=====%@====%@====%@===%@",bankcode,bankeng,bankname,img);
控制台输出.png
单个数据
//代码创建plist文件
// 沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//完整路径
NSString *doucumentPath = [path objectAtIndex:0];
NSString *plistPath = [doucumentPath stringByAppendingPathComponent:@"test.plist"];


//创建数据
NSMutableDictionary * testDic = [NSMutableDictionary dictionary];
[testDic setObject:@"奥迪" forKey:@"车"];
[testDic setObject:@"红色" forKey:@"车色"];
[testDic setObject:@"京A888888" forKey:@"车牌"];
[testDic setObject:@"奥迪A8" forKey:@"车型"];
//写入数据
[testDic writeToFile:plistPath atomically:YES];


NSMutableDictionary * dic11 = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@"==车===%@==车色==%@+===车牌=%@===车型=%@",dic11[@"车"],dic11[@"车色"],dic11[@"车牌"],dic11[@"车型"]);
控制台
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容