主要是这两个帖子html
http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/ios
http://tech.glowing.com/cn/objective-c-runtime/objective-c
而后,关于里面的代码实现有2个比较不错的博客,能够参考ide
http://blog.sunnyxx.comspa
http://www.cnblogs.com/biosli/p/NSObject_inherit_2.htmlcode
另外还能够补充其余一些:orm
//-----------------------------------刨根问底Objective-C Runtime ---------------------htm
就这些基本能搞懂这个runtime的原理了。
本身写了一个小例子:
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