iOS-模型转字典

作者: 请叫我大帅666 | 来源:发表于2019-08-19 19:39 被阅读0次
    //model转化为字典
    - (NSDictionary *)dicFromObject:(NSObject *)object {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        unsigned int count;
        objc_property_t *propertyList = class_copyPropertyList([object class], &count);
        
        for (int i = 0; i < count; i++) {
            objc_property_t property = propertyList[i];
            const char *cName = property_getName(property);
            NSString *name = [NSString stringWithUTF8String:cName];
            NSObject *value = [object valueForKey:name];//valueForKey返回的数字和字符串都是对象
            
            if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [dic setObject:value forKey:name];
                
            } else if ([value isKindOfClass:[NSArray class]]) {
                //数组或字典
                [dic setObject:[self arrayWithObject:value] forKey:name];
            } else if ([value isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[self dicWithObject:value] forKey:name];
            } else if (value == nil) {
                //null
                //[dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????
            } else {
                //model
                [dic setObject:[self dicFromObject:value] forKey:name];
            }
        }
        
        return [dic copy];
    }
    
    
    - (NSArray *)arrayWithObject:(id)object {
        //数组
        NSMutableArray *array = [NSMutableArray array];
        NSArray *originArr = (NSArray *)object;
        if ([originArr isKindOfClass:[NSArray class]]) {
            for (NSObject *object in originArr) {
                if ([object isKindOfClass:[NSString class]]||[object isKindOfClass:[NSNumber class]]) {
                    //string , bool, int ,NSinteger
                    [array addObject:object];
                } else if ([object isKindOfClass:[NSArray class]]) {
                    //数组或字典
                    [array addObject:[self arrayWithObject:object]];
                } else if ([object isKindOfClass:[NSDictionary class]]) {
                    //数组或字典
                    [array addObject:[self dicWithObject:object]];
                } else {
                    //model
                    [array addObject:[self dicFromObject:object]];
                }
            }
            return [array copy];
        }
        return array.copy;
    }
    
    - (NSDictionary *)dicWithObject:(id)object {
        //字典
        NSDictionary *originDic = (NSDictionary *)object;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        if ([object isKindOfClass:[NSDictionary class]]) {
            for (NSString *key in originDic.allKeys) {
                id object = [originDic objectForKey:key];
                if ([object isKindOfClass:[NSString class]]||[object isKindOfClass:[NSNumber class]]) {
                    //string , bool, int ,NSinteger
                    [dic setObject:object forKey:key];
                } else if ([object isKindOfClass:[NSArray class]]) {
                    //数组或字典
                    [dic setObject:[self arrayWithObject:object] forKey:key];
                } else if ([object isKindOfClass:[NSDictionary class]]) {
                    //数组或字典
                    [dic setObject:[self dicWithObject:object] forKey:key];
                } else {
                    //model
                    [dic setObject:[self dicFromObject:object] forKey:key];
                }
            }
            return [dic copy];
        }
        return dic.copy;
    }
    
    
    
    

    相关文章

      网友评论

        本文标题:iOS-模型转字典

        本文链接:https://www.haomeiwen.com/subject/tgzctctx.html