UI-之plist文件解析
来源:互联网 发布:知乎 感情 精华贴 编辑:程序博客网 时间:2024/05/16 10:57
一,简单介绍一下常用的plist文件。
全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。 Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息,不用于与用户交互的信息的存取,用户信息的交互数据的存取一般采用数据进行。 plist文件的实质是XML文件,用于用户信息配置文件1 Plist文件的写入操作1.将数组写入文件plist中``NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@".plist"]; NSString *p=self.regisinfo.text.text; NSString *p1=self.setM.text.text; NSString *p2=self.email.text.text; NSString *name = @"registName"; NSString *name1 = @"SecuM"; NSString *name2 = @"email"; NSDictionary *myDic1=[NSDictionary dictionaryWithObjectsAndKeys:p,name,p1,name1,p2,name2,nil]; // 创建可变数组 NSMutableArray 添加的元素是字典 NSMutableArray *myArr=[[NSMutableArray alloc] initWithObjects:myDic1,nil]; //将数组写入文件plist中 BOOL success = [myArr writeToFile:path atomically:YES]; if (success) { NSLog(@"write success"); [self registFail:@"注册信息" msg:@"注册成功" actionWithTitle:@"确认"]; }2.将字典写入到plist中 NSValue *value = [NSValue valueWithRange:NSMakeRange(1, 5)]; NSDictionary *dic = @{@"key1":@12345,@"key2":@"tttxxx",@"key3":value}; NSString *homePath = NSHomeDirectory(); NSString *path = [homePath stringByAppendingPathComponent:@"t.plist"]; BOOL success = [dic writeToFile:path atomically:YES]; if (success) { NSLog(@"write success"); }2 Plist文件的读出操作 NSString *homePath = NSHomeDirectory(); NSString *path = [homePath stringByAppendingPathComponent:@"test.plist"]; NSArray *array = [NSArray arrayWithContentsOfFile:path]; NSLog(@"%@",array);
3 Plist文件最重要的是对plist文件进行数据对象模型的构建
第一步:构建一个对象userInformation
userInformation.h文件
#import <Foundation/Foundation.h>@interface userInformation : NSObject@property(nonatomic,copy)NSString *name;@property(nonatomic,copy)NSString *info;-(instancetype)initWithdic:(NSDictionary *)dic;+(instancetype)appWithdic:(NSDictionary *)dic;@end
userInformation.m文件
@implementation userInformation-(instancetype)initWithdic:(NSDictionary *)dic{ if (self = [super init]) { self.name = dic[@"name"]; self.info = dic[@"info"]; } return self;}+(instancetype)appWithdic:(NSDictionary *)dic{ return [[self init]initWithdic:dic];}@end
第二步 在控制文件中定义一个数组对象属性,然后重写该属性getter方法,加载plist中的文件,因此管叫这种方法为懒加载,这样后面的程序就可以直接使用了。
@property(nonatomic,retain)NSArray *apps;@end@implementation RegistViewController//采用懒加载数据-(NSArray *)apps{ if (_apps==nil) { NSString *homePath =@"/Users/lan/Desktop/HW-03-3"; NSString *path = [homePath stringByAppendingPathComponent:@"test.plist"]; NSArray *arrayDic = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *arryModle = [NSMutableArray array]; for (id dic in arrayDic) {//这个遍历出来的是字典 userInformation *modle = [[userInformation alloc]initWithdic:dic]; [arryModle addObject:modle]; } _apps =arryModle; } return _apps;}
这样后面的程序如果要使用到plist文件中的信息,直接通过点语法就可以访问了。这样就实现数据跟程序的分离了,plist文件变了,我们只需要修改对象模型就可以了,这样有效的保证了程序的健壮性
0 0
- UI-之plist文件解析
- iOS之OC随笔-plist文件解析
- Xcode7.2简单解析之PList文件解析
- java 解析 plist文件
- android解析plist文件
- android解析plist文件
- android解析plist文件
- java 解析 plist文件
- 大图文件Plist解析
- Android Plist文件解析
- 如何解析Plist文件?
- plist文件解析
- Android开发之:解析plist文件遇到的奇怪问题
- IOS之plist文件
- 怎么解析info.plist文件
- 3、Info.plist文件介绍-OC+UI
- IOS--UI--通讯录实战--Plist 取文件
- lua之读写plist文件
- LintCode 螺旋矩阵 (新解法)
- Scala: Under The Hood of Hello World
- 活了20年多了才知道!蜂蜜十大鲜见真相吓掉小命
- Android 动画杂谈 (三)
- 单源最短路径
- UI-之plist文件解析
- 链表反转的几种形式
- HDU 5583 Kingdom of Black and White(依旧是暴力)——2015ACM/ICPC亚洲区上海站
- 软件测试基础题
- 《如何高效学习》读书笔记
- Oracle程序包
- 用Block 进行pop和传值
- Android开源BezierView:仿QQ未读消息99+条的红色气泡
- 网页设计心得报告(1)by ksy