动态映射objective-c的对象方法修改空指针
来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/03/29 05:28
背景:目前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。
- 动态映射objective-c的对象方法修改空指针
- objective c 的对象指针到底是什么
- objective c 的对象指针到底是什么
- Objective-C空指针与野指针
- 【Objective-C】空指针和野指针
- Objective-C中动态识别的方法
- Objective-C设置修改对象属性方法总结
- Objective-C - 对象作为方法的参数
- Objective-C 面向对象的基本概念——指针
- 【Objective-C】09-空指针和野指针
- 【Objective-C】09-空指针和野指针
- 【Objective-C】09-空指针和野指针
- 【Objective-C】09-空指针和野指针
- 调用String对象的toString()方法时报空指针异常
- objective-c的NSMutableDictionary对象数据修改测试
- objective-C编程 对象的类型和动态绑定摘要
- Objective-C类,对象,方法
- Objective-c - 野指针和僵尸对象
- C#建立VPN
- 正则表达式基础及java使用
- C语言中typedef的用法说明(最好还…
- c语言心得
- 2014年05月11日
- 动态映射objective-c的对象方法修改空指针
- 2014年05月13日
- 2014年05月13日
- 2014年05月13日
- FastDFS介绍说明及安装方法
- 2014年05月14日
- 2014年05月14日
- 2014年05月15日
- sdk manager 遇到 fetch fail的解决办法