runtime之实现方法交换(简单版本)


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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值