IOS中KVC使用注意

来源:互联网 发布:网购除了淘宝还有什么 编辑:程序博客网 时间:2024/06/05 21:54

#import "Person.h"@implementation Person + (instancetype)personWithDict:(NSDictionary *)dict {return [[self alloc] initWithDict:dict];}-(instancetype)initWithDict:(NSDictionary *)dict {if (self = [super init]) {// KVC:会将字典的所有key-value(键值对)赋值给模型对应的属性[self setValuesForKeysWithDictionary:dict];}} @end


@interface Person :NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *phonenumber;@property (nonatomic, assign) int age;+ (instancetype)personWithDict:(NSDictionary *)dict;-(instancetype)initWithDict:(NSDictionary *)dict;@end

注意:(1)假设dict里面的内容比Person里面的字段多则会出现错误。也就是它们必须一一对应(个数和名称)。

0 0
原创粉丝点击