UI-之plist文件解析
2015-11-28 17:47
531 查看
一,简单介绍一下常用的plist文件。
3 Plist文件最重要的是对plist文件进行数据对象模型的构建
第一步:构建一个对象userInformation
userInformation.h文件
userInformation.m文件
第二步 在控制文件中定义一个数组对象属性,然后重写该属性getter方法,加载plist中的文件,因此管叫这种方法为懒加载,这样后面的程序就可以直接使用了。
这样后面的程序如果要使用到plist文件中的信息,直接通过点语法就可以访问了。这样就实现数据跟程序的分离了,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文件变了,我们只需要修改对象模型就可以了,这样有效的保证了程序的健壮性
相关文章推荐
- 【Leetcode】N-Queens II
- UINavigationController设置导航栏属性
- 【Leetcode】N-Queens
- UITabBarController
- Codeforces 288E Polo the Penguin and Lucky Numbers
- UITableView 的结构
- 使用druid连接池的超时回收机制排查连接泄露问题
- 在Legacy BIOS与UEFI 两种模式安装Windows 8操作系统的方法
- iOS 9人机界面指南:UI设计基础--模态情景(Modal Contexts)
- HDFS Short-Circuit Local Reads
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备四)
- easyui datagrid 表格组件列属性formatter和styler使用方法
- 导航栏控制器和标签栏控制器(UINavigationController和UITabBarController)混用
- codeforces B.Queries about less or equal elements
- UIViewController、UINavigationController与UITabBarController的整合使用
- Android属性之build.prop生成过程分析
- 软件测试自动化之- 基于反射的UI自动化测试框架 - UI Automation Test Framework
- H+响应式后台主题UI
- ToStringBuilder、EqualsBuilder、HashCodeBuilder等实例