- 本文代码讲述如何创建一个model
1..h文件:
//属性
@property (nonatomic, copy)NSString * uid;//卖家id
//创建方法
+(id)modelWithDict:(NSDictionary *)dict;
2..m文件:
+(id)modelWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
-initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
//对key进行重命名
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
// if ([key isEqual: @"id"]) {
// self.goods_id = [value longValue];
// }
}
//需要重写这个方法,防止因为后台传递nil值造成的闪退
-(void)setNilValueForKey:(NSString *)key {
// if ([key isEqualToString:@"label"]) {
// _label = "XXX"
// }
}