kvc 定义 model

来源:互联网 发布:淘宝怎么避免虚假交易 编辑:程序博客网 时间:2024/05/21 09:55
//字典类型
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"雨松MOMO",@"name",@"15810463139",@"number", nil];
NSObject *object = [dictionary objectForKey:@"name”];
//kvc 定义 model

-(instancetype)initWithDictionary:(NSDictionary *)dict

 {

    if (self = [super init]) {

//        _answer = dict[@"answer"];

//        _icon = dict[@"icon"];

//        _title = dict[@"title"];

//        _options = dict[@"options”];

        //使用KVC的方式把字典数据直接灌入数据模型

        [self setValuesForKeysWithDictionary:dict];

        _answerCount = _answer.length;

    }

    return self;

}


//指定初始化 与 类方法

+(instancetype)questionWithDictionary:(NSDictionary *)dict{

    return [[self alloc] initWithDictionary:dict];

}

//类方法初始化 与 指定初始化

//            QYQuestion *question = [QYQuestion questionWithDictionary:dict];

            QYQuestion *question = [[QYQuestion alloc]  initWithDictionary:dict];

//block 块儿

@interface QYAnswerView : UIView

//声明 block

@property(strong, nonatomic)void(^answerBtnAction)(UIButton *btn);

//运用block 传值

-(void)btnClick:(UIButton *)sender{

    if (_answerBtnAction) {

        _answerBtnAction(sender);

    }

}

//block 实现

 __weak ViewController *weakSelf = self;

    _answerView.answerBtnAction = ^(UIButton *btn){

        [weakSelf answerBtnClick:btn];

    };




0 0