Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于字符串数组无法自动转换成NSNumber数组的问题 #325

Open
siriusicy opened this issue Dec 17, 2022 · 0 comments
Open

关于字符串数组无法自动转换成NSNumber数组的问题 #325

siriusicy opened this issue Dec 17, 2022 · 0 comments

Comments

@siriusicy
Copy link

虽然知道不再维护了,既然发现了就写一下吧.

///数据
{
    "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);
            }
        }
    
    ///...                
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant