转于:AirZilong的博客
OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法。利用runtime机制让我们可以在程序运行时动态修改类、对象中的所有属性、方法,就算是私有方法以及私有属性都是可以动态修改的。本文旨在对runtime的部分特性小试牛刀,更多更全的方法可以参考系统API文件<objc/runtime.h>,demo例子可以参见CSDN的runtime高级编程系列文章。
先看一个Person类:
Person.h
#import <UIKit/UIKit.h>
@interface Person : NSObject
@property (nonatomic,strong) NSString *name;
- (void)sayHello;
@end ```
Person.m
import "Person.h"
@interface Person()
@property (nonatomic,strong) NSString *address;
@end
@implementation Person
(instancetype)init
{
self = [super init];
if (self) {
_address = @"三里屯SOHO";
self.name = @"AirZilong";
}
return self;
}(NSString *)description
{
return [NSString stringWithFormat:@"address: %@, name: %@",self.address,self.name];
}(void)sayHello
{
NSLog(@"hello ,I'm at %@",self.address);
}(void)interface
{
NSLog(@"I'm %@",self.name);
}
@end
控制器controller.m
import "ViewController.h"
import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *onePerson = [[Person alloc] init];
NSLog(@"first time : %@",[onePerson description]);
#第一步:获取属性列表
unsigned int count = 0;
Ivar *members = class_copyIvarList([Person class], &count);
#第二步:遍历属性
for (int i = 0; i < count; i++)
{
Ivar var = members[i];
# 拿到属性
const charchar *memberAddress = ivar_getName(var);
const charchar *memberType = ivar_getTypeEncoding(var);
NSLog(@"address = %s ; type = %s",memberAddress,memberType);
}
#第三步: 修改私有变量
Ivar m_address = members[1];
object_setIvar(onePerson, m_address, @"朝阳公园");
NSLog(@"second time : %@",[onePerson description]);
# 控制私有函数
[self private];
//
// //添加新方法
// [self addFun];
}
- (void)private
{获取私有方法
unsigned int count = 0;
Method *memberFuncs = class_copyMethodList([Person class], &count);
for (int i = 0; i < count; i++)
{
SEL address = method_getName(memberFuncs[i]);
NSString *methodName = [NSString stringWithCString:sel_getName(address) encoding:NSUTF8StringEncoding];
NSLog(@"member method : %@",methodName);
}
} ```
最后打印出来的内容;
**2016-03-17 23:55:20.833 Test-RunTime[6531:98356] first time : address:** **三里屯****SOHO, name: AirZilong**
**2016-03-17 23:55:20.833 Test-RunTime[6531:98356] address = _name ; type = @"NSString"**
**2016-03-17 23:55:20.833 Test-RunTime[6531:98356] address = _address ; type = @"NSString"**
**2016-03-17 23:55:20.833 Test-RunTime[6531:98356] second time : address:** **朝阳公园****, name: AirZilong**
**2016-03-17 23:55:20.833 Test-RunTime[6531:98356] member method : sayHello**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : address**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : interface**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : description**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : name**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : setName:**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : init**
**2016-03-17 23:55:20.834 Test-RunTime[6531:98356] member method : setAddress:**```