OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法。利用runtime机制让我们可以在程序运行时动态修改类、对象中的所有属性、方法,就算是私有方法以及私有属性都是可以动态修改的。本文旨在对runtime的部分特性小试牛刀,更多更全的方法可以参考系统API文件<objc/runtime.h>,demo例子可以参见CSDN的runtime高级编程系列文章。
废话少说,上代码:
先看一个Person类:
Person.h
[objc] view plaincopy
<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
-
import <UIKit/UIKit.h>
@interface Person : NSObject
@property (nonatomic,strong) NSString *name;
- (void)sayHello;
@end
Person.m
[objc] view plaincopy
<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
-
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
[objc] view plaincopy
<embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
<pre name="code" class="objc">#import "ViewController.h"
-
import "Person.h"
-
import
@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:
是不是很神奇,赶快努力吧!
转于:AirZilong的博客