动态映射objective-c的对象方法修改空指针

背景:目前iOS工程较多将json数据转换成一个对象来储存。假设这个对象有一个Attributes为NSString类型叫myName,这个Attributes一直是nil,创建这对象后忘记对它赋值。现在要通过一个函数来检测他出来,再将他赋值为@“”;


另外,我不知道有多少个Attributes,只要是NSString类型而且Attributes是nil就赋值为@“”。


代码如下:

//

//  GMCommonHelper.h

#import <Foundation/Foundation.h>

#import <objc/runtime.h>



@interface GMCommonHelper : NSObject


+(void)fixNilData:(id)kClass;


@end


//

//  GMCommonHelper.m

#import <AdSupport/AdSupport.h>


#import “GMCommonHelper.h"

@implementation GMCommonHelper


+(void)fixNilData:(id)kClass

{

    unsigned int propertyCount = 0;

    objc_property_t *properties = class_copyPropertyList([kClass class], &propertyCount);

    

    for (unsigned int i = 0; i < propertyCount; ++i) {

        objc_property_t property = properties[i];

        

        const char * name = property_getName(property);

        const char * attrs = property_getAttributes(property);

        NSString * utf8Name = [NSString stringWithUTF8String:name];

        NSString * utf8Attrs = [NSString stringWithUTF8String:attrs];

        id propertVal = [kClass valueForKey:utf8Name];

        if (!propertVal && [utf8Attrs hasPrefix:@"T@\"NSString\""]) {

            [kClass setValue:@"" forKey:utf8Name];

        }

    }

    free(properties);

}



//———————————————————————


- (void)releaseProperties { unsigned int c = 0; objc_property_t *properties = class_copyPropertyList([self class], &c); for(unsigned int i = 0; i < c; i++) { objc_property_t property = properties[i]; NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)]; NSString *propertyType = [NSString stringWithUTF8String:property_getAttributes(property)]; if([propertyType hasPrefix:@"T@"] // is an object && [propertyType rangeOfString:@",R,"].location == NSNotFound // not readonly ) { [self setValue:nil forKey:propertyName]; NSLog(@"%@.%@ = %@", NSStringFromClass(cls), propertyName, [self valueForKey:propertyName]); } } free(properties);}

//———————————————————————



If you only care about classes you can use isKindOfClass:, but if you want to deal with scalars you are correct that you need to use property_getAttributes(), it returns a string that encodes the type information. Below is a basic function that demonstrates what you need to do. For examples of encoding strings, look here.

unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([object class], &outCount);
for (i = 0; i < outCount; i++) {
    objc_property_t property = properties[i];
    char *property_name = property_getName(property);
    char *property_type = property_getAttributes(property);

    switch(property_type[1]) {
      case 'f' : //float
        break;
      case 's' : //short
        break;
      case '@' : //ObjC object
        //Handle different clases in here
        break;
    }
}

Obvviously you will need to add all the types and classes you need to handle to this, it uses the normal ObjC @encode type。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis-plus报空指针异常可能是因为在普通类调用Mapper接口时,使用了mybatis-plus的功能,但在泛型类中找不到id属性,导致查找到的值为空。这会引发空指针异常(NullPointerException)。为了解决这个问题,可以通过深入学习MyBatis并了解其核心对象、配置文件和映射文件的使用来修复。确保在泛型类中定义了正确的id属性,并正确配置映射文件中的语句映射。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot:在普通类调用 Mapper 接口使用 MybatisPlus 报错:空指针异常(NullPointerException)](https://blog.csdn.net/weixin_43642521/article/details/124491271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [MyBatis-plus使用出现空指针异常或者表不纯在](https://blog.csdn.net/gtq1061414470/article/details/121019792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [SSM框架的学习与应用JavaEE(第二天)Mybatis的深入学习](https://download.csdn.net/download/m0_53659738/88241458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值