-
Notifications
You must be signed in to change notification settings - Fork 921
Open
Description
虽然知道不再维护了,既然发现了就写一下吧.
///数据
{
"data": {
"ids": [
"111",
"222"
]
}
}
///Model .h
@interface DataModel : NSObject
@property (nonatomic, copy) NSArray<NSNumber *> *ids;
@end
///.m
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{
@"ids" : [NSNumber class]
};
}
需求: 后台返回字符串数组,需要解析成NSNumber数组,按上面的代码,实际转化后ids数组为空.
如果.m没有实现modelContainerPropertyGenericClass方法,转化后ids为字符串的数组,后续使用大概率崩溃.
PS:同样的,如果后台返回的是数字,需要转化成NSString的数组,也会出现同样的问题.
找了一下,问题出现在
static void ModelSetValueForProperty(__unsafe_unretained id model,
__unsafe_unretained id value,
__unsafe_unretained _YYModelPropertyMeta *meta) {
/// ...
case YYEncodingTypeNSArray:
case YYEncodingTypeNSMutableArray: {
if (meta->_genericCls) {
NSArray *valueArr = nil;
if ([value isKindOfClass:[NSArray class]]) valueArr = value;
else if ([value isKindOfClass:[NSSet class]]) valueArr = ((NSSet *)value).allObjects;
if (valueArr) {
NSMutableArray *objectArr = [NSMutableArray new];
for (id one in valueArr) {
if ([one isKindOfClass:meta->_genericCls]) {
[objectArr addObject:one];
} else if ([one isKindOfClass:[NSDictionary class]]) {
/// ...
}
///todo 这里没有处理基本类型之间的自动转化
///
}
((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, objectArr);
}
}
///...
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels