// // KWMNewProducts.m // iCemarose // // Created by 陈荣科 on 2016/11/8. // Copyright © 2016年 kollway. All rights reserved. // #import "KWMNewProducts.h" #import "KWMNewGoodsModel.h" @implementation KWMNewProducts - (instancetype)initWithDictionary:(NSDictionary *)dict modelClass:(Class)modelClass error:(NSError **)err { NSMutableArray *dictArr = [NSMutableArray array]; dictArr = dict; _collections = [self buildListData:(Class)modelClass jsonDictionary:dictArr]; return self; } - (void)setCollectionsTag{ for (KWMNewGoodsModel *goods in _collections) { goods.tags = @"sale"; } } - (NSArray *)buildListData:(Class)modelClass jsonDictionary:(NSArray *)dataArray { if(modelClass == nil){ NSString *reason = [[NSString alloc] initWithFormat:@"class with name:%@ doesn't exists!!", NSStringFromClass(modelClass)]; @throw [[NSException alloc] initWithName:NSInvalidArgumentException reason:reason userInfo:nil]; } if(! [dataArray isKindOfClass:[NSArray class]]){ NSString *reason = [[NSString alloc] initWithFormat:@"dataArray object is not a NSArray instance"]; @throw [[NSException alloc] initWithName:NSInvalidArgumentException reason:reason userInfo:nil]; } NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; for(NSDictionary *itemJson in dataArray){ NSError *error; id result = [[modelClass alloc] initWithDictionary:itemJson error:&error]; if(error){ NSLog(@"buildListData error: %@", [error localizedDescription]); }else{ if(modelClass == [KWMNewGoodsModel class] && itemJson!=nil && [itemJson objectForKey:@"Comments"]!=nil){ ((KWMNewGoodsModel *)result).comments = [itemJson objectForKey:@"Comments"]; } [mutableArray addObject:result]; } } return [[NSArray alloc] initWithArray:mutableArray]; } @end