继承NSObject
//这里基本上处理服务器返回的数据
//接着上面点评的api写的数据
//这里就简单处理一组数据,其中用到了2个封装(字典转模型)
.h
+(NSArray *)getAllSorts;
.m
//2个封装,前提是从Plist文件中读数据;->数组潜逃字典的样式
-(NSArray *)getAndParseWithPlistFile:(NSString *)plistFileName withClass:(Class)modelClass {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistFileName ofType:nil];
//从plist读取数据
NSArray *plistArray =[NSArray arrayWithContentOfFile:plistPath];
NSArray *returnArray = [self getArrayWithArray :plistArray withClass :modelClass];
return returnArray;
//把数组中的字典循环转成模型对象,并返回数组
-(NSArray *)getArrayWithArray:(NSArray *)array withClass:(Class)modelClass {
//将字典转成模型对象(类型不一样); 返回
NSMutableArray *mutableArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
id model = [modelClass new];
//KVC转换
[model setValuesForKeysWithDictionary:dic];
[mutableArray addObject:model];
}
return [mutableArray copy];
}
网友评论