美文网首页
model模型转字典dic

model模型转字典dic

作者: Anday_ | 来源:发表于2019-08-22 14:03 被阅读0次

    // model模型转字典 实现代码

    -(NSDictionary*)dicFromObject:(NSObject*)object {

        NSMutableDictionary *dic = [NSMutableDictionary dictionary];

        unsignedintcount;

        objc_property_t*propertyList =class_copyPropertyList([objectclass], &count);

        for(inti =0; i < count; i++) {

            objc_property_tproperty = propertyList[i];

            constchar*cName =property_getName(property);

            NSString*name = [NSStringstringWithUTF8String:cName];

            NSObject*value = [objectvalueForKey:name];//valueForKey返回的数字和字符串都是对象

            if([valueisKindOfClass:[NSStringclass]] || [valueisKindOfClass:[NSNumberclass]]) {

                //string , bool, int ,NSinteger

                [dicsetObject:valueforKey:name];

            }elseif([valueisKindOfClass:[NSArrayclass]] || [valueisKindOfClass:[NSDictionaryclass]]) {

                //字典或字典

                [dicsetObject:[selfarrayOrDicWithObject:(NSArray*)value]forKey:name];

            }elseif(value ==nil) {

                //null

                //[dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????

            }else{

                //model

                [dicsetObject:[selfdicFromObject:value]forKey:name];

            }

        }

        return[diccopy];

    }

    //将可能存在model数组转化为普通数组

    -(id)arrayOrDicWithObject:(id)origin {

        if([originisKindOfClass:[NSArrayclass]]) {

            //数组

            NSMutableArray *array = [NSMutableArray array];

            for(NSObject*objectinorigin) {

                if([objectisKindOfClass:[NSStringclass]] || [objectisKindOfClass:[NSNumberclass]]) {

                    //string , bool, int ,NSinteger

                    [arrayaddObject:object];

                }elseif([objectisKindOfClass:[NSArrayclass]] || [objectisKindOfClass:[NSDictionaryclass]]) {

                    //数组或字典

                    [arrayaddObject:[selfarrayOrDicWithObject:(NSArray*)object]];

                }else{

                    //model

                    [arrayaddObject:[selfdicFromObject:object]];

                }

            }

            return[arraycopy];

        }elseif([originisKindOfClass:[NSDictionaryclass]]) {

            //字典

            NSDictionary*originDic = (NSDictionary*)origin;

            NSMutableDictionary *dic = [NSMutableDictionary dictionary];

            for(NSString*keyinoriginDic.allKeys) {

                idobject = [originDicobjectForKey:key];

                if([objectisKindOfClass:[NSStringclass]] || [objectisKindOfClass:[NSNumberclass]]) {

                    //string , bool, int ,NSinteger

                    [dicsetObject:objectforKey:key];

                }elseif([objectisKindOfClass:[NSArrayclass]] || [objectisKindOfClass:[NSDictionaryclass]]) {

                    //数组或字典

                    [dicsetObject:[selfarrayOrDicWithObject:object]forKey:key];

                }else{

                    //model

                    [dicsetObject:[selfdicFromObject:object]forKey:key];

                }

            }

            return[diccopy];

        }

        return[NSNullnull];

    }

    来至:https://blog.csdn.net/minggeqingchun/article/details/80280737

    相关文章

      网友评论

          本文标题:model模型转字典dic

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