Person.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
/**
* 类方法run
*/
+ (void)run;
/**
* 类方法eat
*/
+ (void)eat;
/**
* 对象方法run
*/
- (void)run;
/**
* 对象方法eat
*/
- (void)eat;
@end
Person.m文件
#import "Person.h"
@implementation Person
+ (void)run {
NSLog(@"类方法Person在run");
}
+ (void)eat {
NSLog(@"类方法Person在eat");
}
- (void)run {
NSLog(@"对象方法Person在run");
}
- (void)eat {
NSLog(@"对象方法Person在eat");
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
#import <objc/runtime.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 运行时类方法交换测试
void ClassMethodExchange();
ClassMethodExchange();
//运行时对象方法交换测试
void InstanceMethodExchange();
InstanceMethodExchange();
}
return 0;
}
/**
* 运行时对象方法交换
*/
void InstanceMethodExchange() {
Person *person = [[Person alloc] init];
NSLog(@"对象方法交换之前:");
NSLog(@"Person发送run消息显示:");
[person run];
NSLog(@"Person发送eat消息显示:");
[person eat];
//这里使用的是对象方法交换,方法中使用class_getInstanceMethod
Method m1 = class_getInstanceMethod([Person class], @selector(run));
Method m2 = class_getInstanceMethod([Person class], @selector(eat));
method_exchangeImplementations(m1, m2);
NSLog(@"对象方法交换之后:");
NSLog(@"Person发送run消息显示:");
[person run];
NSLog(@"Person发送eat消息显示:");
[person eat];
}
/**
* 运行时类方法之间交换
*/
void ClassMethodExchange() {
NSLog(@"类方法交换之前:");
NSLog(@"Person发送run消息显示:");
[Person run];
NSLog(@"Person发送eat消息显示:");
[Person eat];
//这里使用的是类方法交换,方法中使用class_getClassMethod
Method m1 = class_getClassMethod([Person class], @selector(run));
Method m2 = class_getClassMethod([Person class], @selector(eat));
/**
* 这里使用的是运行时的方法交换
*/
method_exchangeImplementations(m1, m2);
NSLog(@"类方法交换之后:");
NSLog(@"Person发送run消息显示:");
[Person run];
NSLog(@"Person发送eat消息显示:");
[Person eat];
}
运行结果:
2015-10-04 22:38:51.903 运行时机制交换方法[1059:79336] 类方法交换之前:
2015-10-04 22:38:51.904 运行时机制交换方法[1059:79336] Person发送run消息显示:
2015-10-04 22:38:51.904 运行时机制交换方法[1059:79336] 类方法Person在run
2015-10-04 22:38:51.905 运行时机制交换方法[1059:79336] Person发送eat消息显示:
2015-10-04 22:38:51.905 运行时机制交换方法[1059:79336] 类方法Person在eat
2015-10-04 22:38:51.905 运行时机制交换方法[1059:79336] 类方法交换之后:
2015-10-04 22:38:51.905 运行时机制交换方法[1059:79336] Person发送run消息显示:
2015-10-04 22:38:51.905 运行时机制交换方法[1059:79336] 类方法Person在eat
2015-10-04 22:38:51.906 运行时机制交换方法[1059:79336] Person发送eat消息显示:
2015-10-04 22:38:51.906 运行时机制交换方法[1059:79336] 类方法Person在run
2015-10-04 22:38:51.906 运行时机制交换方法[1059:79336] 对象方法交换之前:
2015-10-04 22:38:51.906 运行时机制交换方法[1059:79336] Person发送run消息显示:
2015-10-04 22:38:51.906 运行时机制交换方法[1059:79336] 对象方法Person在run
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] Person发送eat消息显示:
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] 对象方法Person在eat
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] 对象方法交换之后:
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] Person发送run消息显示:
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] 对象方法Person在eat
2015-10-04 22:38:51.907 运行时机制交换方法[1059:79336] Person发送eat消息显示:
2015-10-04 22:38:51.908 运行时机制交换方法[1059:79336] 对象方法Person在run
Program ended with exit code: 0
使用时注意:
1.导入运行时库文件<objc/runtime.h>
2.类方法使用class_getClassMethod
3.对象方法使用class_getInstanceMethod
交换方法为:method_exchangeImplementations(m1, m2);