.h
@interface BaseModel : NSObject
+ (NSMutableArray *) baseModelByArr: (NSArray *) arr;
@end
.m
@implementation BaseModel
+ (NSMutableArray *) baseModelByArr: (NSArray *) arr {
NSMutableArray *modelArr = [NSMutableArray array];
for (NSDictionary *temp in arr) {
@autoreleasepool {
id model = [[self class] baseModelWithDic:temp];
[modelArr addObject: model];
}
}
return modelArr;
}
+ (instancetype) baseModelWithDic: (NSDictionary *) dic {
id model = [[[self class] alloc] initWithDic:dic];
return [model autorelease];
}
- (instancetype) initWithDic: (NSDictionary *) dic {
if (self = [super init]) {
[self setValuesForKeysWithDictionary: dic];
}
return self;
}
- (void) setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end
eg.
// Movie.h
@interface Movie : BaseModel
@property (nonatomic, copy) NSString *movieId;
@property (nonatomic, copy) NSString *movieName;
@end
// Movie.m
@implementation Movie
- (void)dealloc
{
[_movieId release];
[_movieName release];
[super dealloc];
}
@end
与NetWorkingTool结合使用
- (void) createData {
[NetWorkingTool toolWithURl:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
NSDictionary *dic = result;
NSArray *arr = dic[@"result"];
self.movieArr = [Movie baseModelByArr:arr];
[self.tableView reloadData];
}];
}