UI-之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文件变了,我们只需要修改对象模型就可以了,这样有效的保证了程序的健壮性

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图解AI

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值