Day7

1 kvc

key value coding

-(instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
//        self.answer = dict[@"answer"];
//        self.icon = dict[@"icon"];
//        self.title = dict[@"title"];
//        self.options = dict[@"options"];
        
//        [self setValue:dict[@"answer"] forKeyPath:@"answer"];
//        [self setValue:dict[@"icon"] forKeyPath:@"icon"];
//        [self setValue:dict[@"title"] forKeyPath:@"title"];
//        [self setValue:dict[@"options"] forKeyPath:@"options"];
        
        //kvc
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

2 修改description

相当于toString()

- (NSString *)description
{
    //如果要在开发时,跟踪对象的明细信息,可以重写description方法
    //todo:fail
    return [NSString stringWithFormat:@"<%@: %p> {answer:%@,title:%@,icon:%@,options:%@}",[self class],self,self.answer,self.title,self.icon,self.options];
}

3 添加category类

相当于扩展类的方法 ---》选oc file --》选category

4 getter时候定义的私有变量

@interface QuestionObject()
{
    UIImage *_image;
}


//_image 只是一个私有变量被image使用赋值并返回
-(UIImage *)image{
    if(!_image){
        _image = [UIImage imageNamed:self.icon];
    }
    return _image;
}
@property (nonatomic,strong,readonly) UIImage *iconImage;//public

  

  

原文地址:https://www.cnblogs.com/lihaozhou/p/4363963.html