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

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的原理了。

iOS开发 runtime实现原理以及实际开发中的应用的更多相关文章

  1. iOS开发-Runtime详解

    iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...

  2. [iOS 开发] WebViewJavascriptBridge 从原理到实战 · Shannon's Blog

    前言:iOS 开发中,h5 和原生实现通信有多种方式, JSBridge 就是最常用的一种,各 JSBridge 类库的实现原理大同小异,这篇文章主要是针对当前使用最为广泛的 WebViewJavas ...

  3. 【如何快速的开发一个完整的iOS直播app】(原理篇)

    原文转自:袁峥Seemygo    感谢分享.自我学习 目录 [如何快速的开发一个完整的iOS直播app](原理篇) [如何快速的开发一个完整的iOS直播app](播放篇) [如何快速的开发一个完整的 ...

  4. ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型

    一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...

  5. VR原理讲解及开发入门

    本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持.   1. VR沉浸感和交互作用产生的原理:   在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...

  6. Cordova - 使用Cordova开发iOS应用实战1(配置、开发第一个应用)

    Cordova - 使用Cordova开发iOS应用实战1(配置.开发第一个应用) 现在比较流行使用 html5 开发移动应用,毕竟只要写一套html页面就可以适配各种移动设备,大大节省了跨平台应用的 ...

  7. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  8. OAuth的机制原理讲解及开发流程

    本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...

  9. iOS开发几年了,你清楚OC中的这些东西么!!!?

    iOS开发几年了,你清楚OC中的这些东西么!!!? 前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1 ...

随机推荐

  1. ng-book札记——内置指令

    Angular提供了一些内置指令(directive),这些添加在HTML元素(element)上的特性(attribute)可以赋予更多的动态行为. NgIf ngIf指令用于在某个条件下显示或者隐 ...

  2. ACM Doing Homework again

    Ignatius刚刚从第30届ACM / ICPC回到学校.现在他有很多作业要做.每个老师给他一个截止作业的截止日期.如果Ignatius在截止日期之后进行了家庭作业,老师将减少他的最终考试成绩.现在 ...

  3. django之数据库orm

    一.数据库的配置 1 django默认支持sqlite,mysql, oracle,postgresql数据库. <1>sqlite django默认使用sqlite的数据库,默认自带sq ...

  4. 自定义下拉刷新上拉加载View

    MainActivity.java package com.heima52.pullrefresh; import java.util.ArrayList; import com.heima52.pu ...

  5. iOS学习笔记--Quartz2D

    Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统. Quartz 2D能完成的工作: 1. 绘制图形 : 线条\三角形\矩形\圆\弧等 2. 绘制文字 3. 绘制\生成图片(图像) 4 ...

  6. django-redis

    linuxapt-get install redis-serverpip install django-redis vim /etc/redis/redis.conf maxmemory 20mb s ...

  7. “出错了”和报告Bug的艺术

    "出错了." 没有那句话能像"出错了"一样让程序员/开发者如此沮丧,心里翻江倒海,怒火一点即燃,还要死掉一大片脑细胞. 这句生硬的开场白通常标志着让开发者恐惧的 ...

  8. 第一行代码阅读笔记---详解分析第一个Android程序

    以下是我根据作者的思路,创建的第一个Android应用程序,由于工具强大,代码都自动生成了,如下: package com.example.first_app; import android.os.B ...

  9. webStorm破解

    B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiw ...

  10. Shell命令:echo 命令详解

    http://blog.chinaunix.net/uid-27124799-id-3383327.html # echo命令介绍 功能说明:显示文字. 语 法:echo [-ne][字符串] / e ...