美文网首页iOS开发
iOS中plist文件

iOS中plist文件

作者: 追逐_chase | 来源:发表于2019-04-12 17:00 被阅读0次

plist文件

  • plist文件储存本地数据的一种方式
  • json实例对象,字典或者数组 跟目录

写入数据到plist文件

  NSArray *names = @[@"zhangsan",@"lisi",@"wangwu",@"mazi"];
  //路径,文件的后缀名必须是  .plist
  [names writeToFile:@"xx/xx/name.plist" atomically:YES];

解析plit文件

    //读取plist文件的路径
   NSString *path = [[NSBundle mainBundle] pathForResource:@"" ofType:@"plist"];
   //根据plist的数据类型进行解析
   //1.根目录是数组./字典
   NSArray *data = [NSArray arrayWithContentsOfFile:path];
   //字典
   NSDictionary *dicData = [NSDictionary dictionaryWithContentsOfFile:path];

模型数据

  • 我们在加载本地plist文件或者json文件数据时,一般的形式是根目录是一个array或者dictionary
  • 在处理array/dictionay的时候
  • 这里就要引出 数据模型的概念 KVC

字典转模型

  • 模型里面的属性要和字典里面的key是一一对应的
  • 模型可以添加自己的辅助属性,但是要实现一个对应的方法 - (void)setValue:(id)value forUndefinedKey:(NSString *)key

模型

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface CCShopModel : NSObject

/*
 这个模型里面的属性和字典中的key是一一对应的
 */

@property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSString *price;

@property (nonatomic, strong) NSString *image;

//对象方法
- (instancetype)initShopDictionatyChangeModelWithDic:(NSDictionary *)dic;

///类方法
+ (instancetype)shopDictionatyChangeModelWithDic:(NSDictionary *)dic;

@end

NS_ASSUME_NONNULL_END


  • CCShopModel.m文件
#import "CCShopModel.h"

@implementation CCShopModel


- (instancetype)initShopDictionatyChangeModelWithDic:(NSDictionary *)dic {
    if (self = [super init]) {
    
        [self setValuesForKeysWithDictionary:dic];
        
    }
    
    return self;
}


+ (instancetype)shopDictionatyChangeModelWithDic:(NSDictionary *)dic{
    
    return [[self alloc]  initShopDictionatyChangeModelWithDic:dic];
}


//m找不到对应的key 走这个方法
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

  • 字典转化模型
  NSArray *datas = @[
                      @{
                           @"name":@"箱包",
                           @"price":@"800.0",
                           @"image":@"xxxxx.png"
                        }
                     ];
    
    
    NSMutableArray *shopData = [NSMutableArray array];
    
    for (NSDictionary *dic in datas) {
        CCShopModel *shopModel = [CCShopModel shopDictionatyChangeModelWithDic:dic];
        [shopData addObject:shopModel];
    }
    

自定义控件封装

  • OC语言的3大特性,继承,封装,多态
  • 封装就是把一个控件集成类,仅对外界提供一些属性或者方法,其内部的子控件不对外开放

自定义一个CCView

  • 我们在CCView.h文件中
    #import <UIKit/UIKit.h>
    
    @class CCShop;
   
   @interface CCView : UIView
   /** 商品模型 */
   @property (nonatomic, strong) CCShop *shop;
   
   // 构造方法
   - (instancetype)initWithShop: (CCShop *)shop;
   
   + (instancetype)ccViewWithShop: (CCShop *)shop;
   @end
  • 注意我们 CCView.m文件 中
    - 定义自己的子控件,比如UIImagebView/UIlabel/UIButton等
    - 定义这些子控件的方式可以是 懒加载,也可以是直接创建

  • 初始化

    #import "CCView.h"
    @interface CCView ()
   /** 图片控件 */
   @property (nonatomic, weak) UIImageView *iconView;
   /** 标题控件 */
   @property (nonatomic, weak) UILabel *titleLabel;
   
   @end
   
   //初始化方法
- (instancetype)init{
   self = [super init];
   if (self) {
        [self setupChild];
   }
   
   return self;
}
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
        [self setupChild];
   }
   
   return self;
}
//重写初始化方法
- (instancetype)initWithShop:(CCShopModel *)shop {
   self = [super init];
   if (self) {
       [self setupChild];
       //设置数据
       self.shop = shop;
   }
   
   return self;
}
//类方法
+ (instancetype)ccViewWithShop:(CCShopModel *)shop{
   
   return [[self alloc] initWithShop:shop];
}
//布局子控件
- (void)setupChild{
   // 1.创建商品的UIImageView对象
   UIImageView *iconView = [[UIImageView alloc] init];
   iconView.backgroundColor = [UIColor blueColor];
   [self addSubview:iconView];
   _iconView = iconView;
   
   // 2.创建商品标题对象
   UILabel *titleLabel = [[UILabel alloc] init];
   titleLabel.backgroundColor = [UIColor yellowColor];
   titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
   [self addSubview:titleLabel];
   _titleLabel = titleLabel;
}
//设置子控件的frame
- (void)layoutSubviews {
   [super layoutSubviews];
   
   // 1.获取当前控件的尺寸
   CGFloat width = self.frame.size.width;
   CGFloat height = self.frame.size.height;
   
   // 2.设置子控件的frame
   self.iconView.frame = CGRectMake(0, 0, width, width);
   self.titleLabel.frame = CGRectMake(0, width, width, height - width);
   
}

/**
*  set方法:只要外边传数据就会调用
*  作用:设置数据
*/
- (void)setShop:(CCShopModel *)shop{
   _shop = shop;
   
   // 设置数据
   self.iconView.image = [UIImage imageNamed:shop.image];
   self.titleLabel.text = shop.name;
}
@end

相关文章

网友评论

    本文标题:iOS中plist文件

    本文链接:https://www.haomeiwen.com/subject/csvlvqtx.html