自己写了一个小例子:有一些相关知识点和博客文章

A: 首先现在控制器里面初始化一个对象,然后调用对象的方法:

#import "ViewController.h"
#import "Message.h"
#import "NSObject+AssociatedObject.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Message *message = [Message new];
    [message sendMessage:@"Sam Lau"];
    
}

@end

B: 对象First的声明:

//
//  Message.h
//  RuntimeDemo
//
//  Created by caijunrong on 7/15/15.
//  Copyright © 2015 caijunrong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Message : NSObject

- (void)sendMessage:(NSString *)word;

@end

对象First的实现:

//
//  Message.m
//  RuntimeDemo
//
//  Created by caijunrong on 7/15/15.
//  Copyright © 2015 caijunrong. All rights reserved.
//

#import "Message.h"
#import "MessageForwarding.h"
#import <objc/runtime.h>

@implementation Message

//- (void)sendMessage:(NSString *)word
//{
//    NSLog(@"normal way : send message = %@", word);
//}

//- (void)sendOtherMessage:(NSString *)word{
//    NSLog(@"sendOtherMessage word:%@",word);
//}

#pragma mark - Method Resolution
/// override resolveInstanceMethod or resolveClassMethod for changing sendMessage method implementation
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
    if (sel == @selector(sendMessage:)) {
        
        //如果是这个方法的话,重新定义一个新的方法,映射过去
        class_addMethod([self class], sel, imp_implementationWithBlock(^(id self, NSString *word) {
            NSLog(@"word = %@", word);
            //通过这种方法可以讲找不到的方法重新定义到别的方法去
            [self sendOtherMessage:word];
        }), "v@*");
    }
    return YES;
}

#pragma mark - Fast Forwarding
//- (id)forwardingTargetForSelector:(SEL)aSelector
//{
//    if (aSelector == @selector(sendMessage:)) {
//        return [MessageForwarding new];
//    }
//    
//    return nil;
//}

#pragma mark - Normal Forwarding
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    NSMethodSignature *methodSignature = [super methodSignatureForSelector:aSelector];
    
    if (!methodSignature) {
        methodSignature = [NSMethodSignature signatureWithObjCTypes:"v@:*"];
    }
    
    return methodSignature;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    MessageForwarding *messageForwarding = [MessageForwarding new];
    
    if ([messageForwarding respondsToSelector:anInvocation.selector]) {
        [anInvocation invokeWithTarget:messageForwarding];
    }
}

@end

对象Second的声明:

//
//  MessageForwarding.h
//  RuntimeDemo
//
//  Created by caijunrong on 7/15/15.
//  Copyright © 2015 caijunrong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MessageForwarding : NSObject

- (void)sendMessage:(NSString *)word;
- (void)sendOtherMessage:(NSString *)word;
@end

对象Second的实现:

//
//  MessageForwarding.m
//  RuntimeDemo
//
//  Created by caijunrong on 7/15/15.
//  Copyright © 2015 caijunrong. All rights reserved.
//

#import "MessageForwarding.h"

@implementation MessageForwarding

- (void)sendMessage:(NSString *)word
{
    NSLog(@"fast forwarding way : send message = %@", word);
}

- (void)sendOtherMessage:(NSString *)word{
    NSLog(@"MessageForwarding sendOtherMessage word:%@",word);
}

@end

相关文章:

http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/

http://tech.glowing.com/cn/objective-c-runtime/

然后,关于里面的代码实现有2个比较不错的博客,可以参考

http://blog.sunnyxx.com

http://www.cnblogs.com/biosli/p/NSObject_inherit_2.html

另外还可以补充其他一些:

//-----------------------------------刨根问底Objective-C Runtime ---------------------

http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime(1)%5Bnil%5D-self-and-super/

http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and-class-and-meta-class/

http://chun.tips/blog/2014/11/06/bao-gen-wen-di-objective%5Bnil%5Dc-runtime(3)%5Bnil%5D-xiao-xi-he-category/

http://chun.tips/blog/2014/11/08/bao-gen-wen-di-objective%5Bnil%5Dc-runtime(4)%5Bnil%5D-cheng-yuan-bian-liang-yu-shu-xing/

就这些基本能搞懂这个runtime的原理了。

最新文章

  1. 让IE系列支持HTML5的html5shiv.js和respond.min.js
  2. Android Studio 使用Lambda
  3. UVM中的class--2
  4. mysql ALL_O_DIRECT引发的unaligned AIO/DIO导致hang
  5. 二分+DP HDU 3433 A Task Process
  6. swt byte[] 与 Image的转换
  7. 通过ajax提交form表单
  8. qsort,mergesort,插入排序
  9. 【NOIP TG 解方程】
  10. Cubieboard 开箱和入门 | Name5566 分类: cubieboard 2014-11-08 17:27 251人阅读 评论(0) 收藏
  11. oracle备份表
  12. Delphi使用XmlHttp获取时间
  13. Mysql 库、表、字段 字符集
  14. Linux终端颜色和标题设置
  15. hdu4585 &amp;amp; BestCoder Round #1 项目管理(vector应用)
  16. React学习总结(一)
  17. Java中的String,StringBuilder,StringBuffer三者的区别(转载)
  18. Kafka并不难学
  19. LNMP(一)
  20. Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

热门文章

  1. python笔记十五(面向对象及其特性)
  2. Android二维码扫描、生成
  3. Programming In Scala笔记-第六章、函数式对象
  4. 剑指Offer——中国银行面试知识储备
  5. 2017年校园招聘ios面试题
  6. The Zen Programmer
  7. Android通过WebService实现图片的上传和下载(一)
  8. iOS界面不能点击(tableView 的cell 不能使用点击事件,tableView也不能上下滚动)
  9. Xcode7.3.1中SKAudioNode在Scene转换后无声的问题
  10. 安卓Button-TextView-EditText综合运用