各种对象转为json字符串

本文介绍了一种使用Objective-C将自定义模型转换为字典的方法。通过遍历模型的所有属性,并将其值设置到NSMutableDictionary中,实现了模型对象到字典的转换。此方法适用于iOS应用开发中对象序列化的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//对象转化为字典

-(NSMutableDictionary *)onereturnToDictionaryWithModel:(ScorelistTwo *)model

{

    NSMutableDictionary *userDic = [NSMutableDictionary dictionary];

    unsigned int count = 0;

    objc_property_t *properties = class_copyPropertyList([ScorelistTwo class], &count);

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

        const char *name = property_getName(properties[i]);

        NSString *propertyName = [NSString stringWithUTF8String:name];

        id propertyValue = [model valueForKey:propertyName];

        if (propertyValue) {

            [userDic setObject:propertyValue forKey:propertyName];

        }

    }

    free(properties);

    return userDic;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值