diff --git a/YYModel/NSObject+YYModel.h b/YYModel/NSObject+YYModel.h old mode 100644 new mode 100755 index 73f62ec..4b6d6c0 --- a/YYModel/NSObject+YYModel.h +++ b/YYModel/NSObject+YYModel.h @@ -102,6 +102,15 @@ NS_ASSUME_NONNULL_BEGIN */ + (nullable instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary; + +/** + Creates and returns a new instance of the receiver from an array containing key-value dictionary, automatic parsing multiple array, if contain anything else element except dictionary, method will return a uninitialized instance. + + @param keyValueArray An array containing key-value dictionary mapped to the instance's properties. + @return A new array containing instance. + */ ++ (NSMutableArray *)yy_modelArrayWithKeyValueArray:(NSArray *)keyValueArray; + /** Set the receiver's properties with a json object. diff --git a/YYModel/NSObject+YYModel.m b/YYModel/NSObject+YYModel.m old mode 100644 new mode 100755 index 3d7c470..0e10f35 --- a/YYModel/NSObject+YYModel.m +++ b/YYModel/NSObject+YYModel.m @@ -1454,6 +1454,28 @@ + (instancetype)yy_modelWithJSON:(id)json { return [self yy_modelWithDictionary:dic]; } ++ (NSMutableArray *)yy_modelArrayWithKeyValueArray:(NSArray *)keyValueArray { + if (!keyValueArray || keyValueArray == (id)kCFNull) return nil; + if (![keyValueArray isKindOfClass:[NSArray class]]) return nil; + NSMutableArray *modelArray = [NSMutableArray array]; + return [self _yy_modelArrayWithRecurArray:keyValueArray modelArray:modelArray];; +} + ++ (NSMutableArray *)_yy_modelArrayWithRecurArray:(NSArray *)array modelArray:(NSMutableArray *)modelArray { + [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + if ([obj isKindOfClass:[NSArray class]]) { + NSMutableArray *array = [NSMutableArray array]; + [modelArray addObject:[self _yy_modelArrayWithRecurArray:obj modelArray:array]]; + } else { + if (![obj isKindOfClass:[NSDictionary class]]) { + obj = [NSDictionary dictionary]; + } + [modelArray addObject:[self yy_modelWithDictionary:obj]]; + } + }]; + return modelArray; +} + + (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary { if (!dictionary || dictionary == (id)kCFNull) return nil; if (![dictionary isKindOfClass:[NSDictionary class]]) return nil;