iOS開發 runtime實現原理以及實際開發中的應用

主要是這兩個帖子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

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/blog

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

 

本身寫了一個小例子:

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
相關文章
相關標籤/搜索