iOS开发 runtime实现原理以及实际开发中的应用
2016-07-21 11:49
567 查看
自己写了一个小例子:有一些相关知识点和博客文章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"]; } @endB: 对象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的原理了。
相关文章推荐
- Python动态类型的学习---引用的理解
- 100 个最佳 Ubuntu 应用(中)
- 在 AppImage、Flathub 和 Snapcraft 平台上搜索 Linux 应用
- 24 个必备的 Linux 应用程序
- 新注册
- 注册表趣味应用小集
- 远程控制技术的应用
- 土人系列AS入门教程 -- 对象篇
- 路由器访问列表的应用
- xDSL技术及其应用
- C#托管堆对象实例包含内容分析
- 四大漏洞入侵博客
- C#实现获取不同对象中名称相同属性的方法
- 基于XML的桌面应用
- Java Runtime Environment 5.0 Update 12 下载
- javascript asp教程第十一课--Application 对象
- SQL Server 2008 R2 应用及多服务器管理
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- VBS教程:对象-正则表达式(RegExp)对象
- C#检查指定对象是否存在于ArrayList集合中的方法