5、object_setIvar 赋值函数
下面我们来看一下代码实现:
1、创建一个类
CustomClass.h
#import <Foundation/Foundation.h> @interface CustomClass : NSObject @property (nonatomic , strong) NSString *age; - (void)test; @end
#import "CustomClass.h" @implementation CustomClass - (void)test { NSLog(@"这里是CustomClass"); } @end
接下来创建一个类别来实现RunTime机制
NSObject+RunTimeTest.h
#import <Foundation/Foundation.h> @interface NSObject (RunTimeTest) //动态创建一个类,之后为这个类的age属性赋值。 - (id) testRunTime:(NSString *)classname age:(NSString *)age; @end
#import "NSObject+RunTimeTest.h" #import <objc/runtime.h> #import "CustomClass.h" @implementation NSObject (RunTimeTest) - (CustomClass *)testRunTime:(NSString *)classname age:(NSString *)age { // propertyCount 成员属性的数量 unsigned int propertyCount = 0; /* objc_property_t 根据苹果文档的解释:An opaque type that represents an Objective-C declared property. 个人理解:一个任意类型的对象代表一个oc声明的属性成员 (本人英语水平实在有限)*/ /* class_copyPropertyList 文档:Return Value An array of pointers of type objc_property_t describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free(). 个人理解:主要2个意思:返回一个数组指针,类型为objc_property_t,用完要记得free,释放掉。 */ objc_property_t *properties = class_copyPropertyList([self class], &propertyCount); for (unsigned int i = 0; i < propertyCount; i++) { objc_property_t property = properties[i]; //获取成员的名称 NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; NSLog(@"propertyName = %@ -- 成员名称",propertyName); //获取成员内容的Ivar Ivar iVar = class_getInstanceVariable([self class], [propertyName UTF8String]); //其实上面那行获取代码是为了保险起见,基本是获取不到内容的。因为成员的名称默认会在前面加"_" , if (iVar == nil) { iVar = class_getInstanceVariable([self class], [[NSString stringWithFormat:@"_%@",propertyName] UTF8String]); } // 取值 id propertyVal = object_getIvar(self, iVar); NSLog(@"propertyVal = %@ --值",propertyVal); // 动态创建类 Class varClass = NSClassFromString(classname); id varObj = [[varClass alloc] init]; //调用新创建类的方法,这里要主要一定引入对应得头文件,不然调用方法会不错。 [varObj test]; Ivar iVarObj = class_getInstanceVariable(varClass, [@"_age" UTF8String]); object_setIvar(varObj, iVarObj, age); return varObj; } return nil; } @end
ViewController.m
#import "NSObject+RunTimeTest.h" #import "CustomClass.h" @interface ViewController () @property (nonatomic , strong) NSString *name; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _name = @"xiaonan"; CustomClass *tmp = [self testRunTime:@"CustomClass" age:@"098"]; NSLog(@"tmp.age = %@",tmp.age); }
结果输出:
2014-11-29 17:16:55.483 TestRunTime2[21001:176367] propertyName = name -- 成员名称 2014-11-29 17:16:55.483 TestRunTime2[21001:176367] propertyVal = xiaonan --值 2014-11-29 17:16:55.484 TestRunTime2[21001:176367] 这里是CustomClass 2014-11-29 17:16:55.484 TestRunTime2[21001:176367] tmp.age = 098
原文地址:http://blog.csdn.net/qqmcy/article/details/41596725
原文地址:http://blog.csdn.net/qqmcy/article/details/41596725