iOS开发

iOS中plist文件

2019-04-12  本文已影响0人  追逐_chase

plist文件

写入数据到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];

模型数据

字典转模型

模型

#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


#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];
    }
    

自定义控件封装

自定义一个CCView

    #import <UIKit/UIKit.h>
    
    @class CCShop;
   
   @interface CCView : UIView
   /** 商品模型 */
   @property (nonatomic, strong) CCShop *shop;
   
   // 构造方法
   - (instancetype)initWithShop: (CCShop *)shop;
   
   + (instancetype)ccViewWithShop: (CCShop *)shop;
   @end
    #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

上一篇 下一篇

猜你喜欢

热点阅读