青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

羅朝輝(飄飄白云)

關(guān)注嵌入式操作系統(tǒng),移動平臺,圖形開發(fā)。-->加微博 ^_^

  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  85 隨筆 :: 0 文章 :: 169 評論 :: 0 Trackbacks
[Cocoa]深入淺出Cocoa之 Method Swizzling
羅朝輝(http://www.shnenglu.com/kesalin)
CC許可,轉(zhuǎn)載請注明出處

在前文深入淺出Cocoa之消息中,我簡要介紹了ObjC 中消息的基本情況,包括SEL查找,緩存以及消息轉(zhuǎn)發(fā)等。在本文中,我要介紹一個很有趣的技術(shù),Method swizzling,通過這個手法,我們可以動態(tài)修改方法的實現(xiàn),從而達到修改類行為的目的。當然,還有其他辦法(如 ClassPosing,Category)也可以達到這個目的。ClassPosing 是針對類級別的,是重量級的手法,Category 也差不多,比較重量級,此外 Category 還無法避免下面的遞歸死循環(huán)(如果你的代碼出現(xiàn)了如下形式的遞歸調(diào)用,應(yīng)該考慮一下你的設(shè)計,而不是使用在這里介紹的 Method Swizzling 手法,:))。

// Bar
//
@implementation Bar

- (void) testMethod
{
    NSLog(@" >> Bar testMethod");
}

@end

// Bar(BarCategory)
//
@implementation Bar(BarCategory)

- (void) altRecursionMethod
{
    NSLog(@" >> Bar(BarCategory) recursionMethod");
    [self altRecursionMethod];
}

@end

在前文深入淺出Cocoa之消息中提到,ObjC 中的類(class)和實例(instance)都是對象,類對象有自己的類方法列表,實例對象有自己的實例方法列表,這些方法列表(struct objc_method_list)是存儲在 struct objc_class 中的。每個方法列表存儲近似 SEL:Method 的對,Method 是一個對象,包含方法的具體實現(xiàn) impl。由此可知,我們只需要修改 SEL 對應(yīng)的 Method 的 impl 既可以達到修改消息行為的目的。下面來看代碼:

void PerformSwizzle(Class aClass, SEL orig_sel, SEL alt_sel, BOOL forInstance)
{
    // First, make sure the class isn't nil
    if (aClass != nil) {
        Method orig_method = nil, alt_method = nil;

        // Next, look for the methods
        if (forInstance) {
            orig_method = class_getInstanceMethod(aClass, orig_sel);
            alt_method = class_getInstanceMethod(aClass, alt_sel);
        } else {
            orig_method = class_getClassMethod(aClass, orig_sel);
            alt_method = class_getClassMethod(aClass, alt_sel);
        }

        // If both are found, swizzle them
        if ((orig_method != nil) && (alt_method != nil)) {
            IMP temp;

            temp = orig_method->method_imp;
            orig_method->method_imp = alt_method->method_imp;
            alt_method->method_imp = temp;
        } else {
#if DEBUG
            NSLog(@"PerformSwizzle Error: Original %@, Alternate %@",(orig_method == nil)?@" not found":@" found",(alt_method == nil)?@" not found":@" found");
#endif
        }
    } else {
#if DEBUG
        NSLog(@"PerformSwizzle Error: Class not found");
#endif
    }
}

void MethodSwizzle(Class aClass, SEL orig_sel, SEL alt_sel)
{
    PerformSwizzle(aClass, orig_sel, alt_sel, YES);
}

void ClassMethodSwizzle(Class aClass, SEL orig_sel, SEL alt_sel)
{
    PerformSwizzle(aClass, orig_sel, alt_sel, NO);
}

讓我們來分析上面代碼:
1,首先,區(qū)分類方法和實例方法;
2,取得 SEL 對應(yīng)的 Method;
3,修改 Method 的 impl,在這里是通過交換實現(xiàn)的。

上面的代碼是可以工作的,但還不夠完善。Apple 10.5 提供了交換 Method 實現(xiàn)的 API: method_exchangeImplementations 。下面我們使用這個新 API,并以 NSObject category的形式給出新的實現(xiàn)方式:

#if TARGET_OS_IPHONE
#import <objc/runtime.h>
#import <objc/message.h>
#else
#import <objc/objc-class.h>
#endif

// NSObject (MethodSwizzlingCategory)
//
@interface NSObject (MethodSwizzlingCategory)

+ (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel;
+ (BOOL)swizzleClassMethod:(SEL)origSel withClassMethod:(SEL)altSel;

@end

@implementation NSObject (MethodSwizzlingCategory)

+ (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel
{
    Method origMethod = class_getInstanceMethod(self, origSel);
    if (!origSel) {
        NSLog(@"original method %@ not found for class %@", NSStringFromSelector(origSel), [self class]);
        return NO;
    }
    
    Method altMethod = class_getInstanceMethod(self, altSel);
    if (!altMethod) {
        NSLog(@"original method %@ not found for class %@", NSStringFromSelector(altSel), [self class]);
        return NO;
    }
    
    class_addMethod(self,
                    origSel,
                    class_getMethodImplementation(self, origSel),
                    method_getTypeEncoding(origMethod));
    class_addMethod(self,
                    altSel,
                    class_getMethodImplementation(self, altSel),
                    method_getTypeEncoding(altMethod));
    
    method_exchangeImplementations(class_getInstanceMethod(self, origSel), class_getInstanceMethod(self, altSel));

    return YES;
}

+ (BOOL)swizzleClassMethod:(SEL)origSel withClassMethod:(SEL)altSel
{
    Class c = object_getClass((id)self);
    return [c swizzleMethod:origSel withMethod:altSel];
}

@end

代碼就不用多解釋了,下面我們來看如何使用。先看輔助類Foo:
Foo.h
//
//  Foo.h
//  MethodSwizzling
//
//  Created by LuoZhaohui on 1/5/12.
//  Copyright (c) 2012 http://www.shnenglu.com/kesalin/. All rights reserved.
//

#import <Foundation/Foundation.h>

// Foo
//
@interface Foo : NSObject

- (void) testMethod;
- (void) baseMethod;
- (void) recursionMethod;

@end

// Bar
//
@interface Bar : Foo

- (void) testMethod;

@end

// Bar(BarCategory)
//
@interface Bar(BarCategory)

- (void) altTestMethod;
- (void) altBaseMethod;
- (void) altRecursionMethod;

@end

Foo.m
//
//  Foo.m
//  MethodSwizzling
//
//  Created by LuoZhaohui on 1/5/12.
//  Copyright (c) 2012 . All rights reserved.
//

#import "Foo.h"

// Foo
//
@implementation Foo

- (void) testMethod
{
    NSLog(@" >> Foo testMethod");
}

- (void) baseMethod
{
    NSLog(@" >> Foo baseMethod");
}

- (void) recursionMethod
{
    NSLog(@" >> Foo recursionMethod");
}

@end

// Bar
//
@implementation Bar

- (void) testMethod
{
    NSLog(@" >> Bar testMethod");
}

@end

// Bar(BarCategory)
//
@implementation Bar(BarCategory)

- (void) altTestMethod
{
    NSLog(@" >> Bar(BarCategory) altTestMethod");
}

- (void) altBaseMethod
{
    NSLog(@" >> Bar(BarCategory) altBaseMethod");
}

- (void) altRecursionMethod
{
    NSLog(@" >> Bar(BarCategory) recursionMethod");
    [self altRecursionMethod];
}

@end

下面是具體的使用示例:
// Main
//
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        Foo * foo = [[[Foo alloc] init] autorelease];
        Bar * bar = [[[Bar alloc] init] autorelease];
        
        NSLog(@"========= Method Swizzling test 1 =========");
        
        NSLog(@" Step 1");
        [foo testMethod];
        [bar testMethod];
        [bar altTestMethod];
        
        NSLog(@" Step 2");
        [Bar swizzleMethod:@selector(testMethod) withMethod:@selector(altTestMethod)];
        [foo testMethod];
        [bar testMethod];
        [bar altTestMethod];
        
        NSLog(@"========= Method Swizzling test 2 =========");
        NSLog(@" Step 1");
        [foo baseMethod];
        [bar baseMethod];
        [bar altBaseMethod];
        
        NSLog(@" Step 2");
        [Bar swizzleMethod:@selector(baseMethod) withMethod:@selector(altBaseMethod)];
        [foo baseMethod];
        [bar baseMethod];
        [bar altBaseMethod];
        
        NSLog(@"========= Method Swizzling test 3 =========");
        [Bar swizzleMethod:@selector(recursionMethod) withMethod:@selector(altRecursionMethod)];
        [bar recursionMethod];
    }

    return 0;
}

輸出結(jié)果為下表。注意,test 3 中調(diào)用了遞歸調(diào)用“自己”的方法,你能理解為什么沒有出現(xiàn)死循環(huán)么?
========= Method Swizzling test 1 =========
  Step 1
  >> Foo testMethod
  >> Bar testMethod
  >> Bar(BarCategory) altTestMethod
  Step 2
  >> Foo testMethod
  >> Bar(BarCategory) altTestMethod
  >> Bar testMethod
========= Method Swizzling test 2 =========
  Step 1
  >> Foo baseMethod
  >> Foo baseMethod
  >> Bar(BarCategory) altBaseMethod
  Step 2
  >> Foo baseMethod
  >> Bar(BarCategory) altBaseMethod
  >> Foo baseMethod
 ========= Method Swizzling test 3 =========
  >> Bar(BarCategory) recursionMethod
  >> Foo recursionMethod

test3 解釋:在函數(shù)體 {} 之間的部分是真正的 IMP,而在這之前的是 SEL。通常情況下,SEL 是與 IMP 匹配的,但在 swizzling 之后,情況就不同了。下圖就是調(diào)用的時序圖。


rentzsch 寫了一個完善的開源類 jrswizzle 來處理 Method Swizzling,如果你在工程中使用到 Method Swizzling 手法,應(yīng)該優(yōu)先使用這個類庫,:)。

Refference:
MethodSwizzling:http://www.cocoadev.com/index.pl?ExtendingClasses
jrswizzle:https://github.com/rentzsch/jrswizzle

posted on 2012-01-05 17:02 羅朝輝 閱讀(2548) 評論(4)  編輯 收藏 引用 所屬分類: 移動開發(fā)

評論

# re: [Cocoa]深入淺出Cocoa之 Method Swizzling 2012-01-05 17:29 marvin
你的博不錯,加油  回復(fù)  更多評論
  

# re: [Cocoa]深入淺出Cocoa之 Method Swizzling 2012-01-05 20:09 羅朝輝
@marvin

多謝鼓勵,一起加油~  回復(fù)  更多評論
  

# re: [Cocoa]深入淺出Cocoa之 Method Swizzling 2012-01-06 15:47 marvin
@羅朝輝
我的博
http://cnblogs.com/chrome
  回復(fù)  更多評論
  

# re: [Cocoa]深入淺出Cocoa之 Method Swizzling 2012-08-08 22:31 拉拉
Method 定義找不到啊
  回復(fù)  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美色区777第一页| 欧美日韩hd| 在线视频观看日韩| 美国十次成人| 欧美激情第六页| 亚洲午夜精品17c| 一区二区久久| 国产一区二区三区不卡在线观看 | 欧美+日本+国产+在线a∨观看| 禁断一区二区三区在线| 免费日韩av片| 欧美日韩免费高清一区色橹橹| 亚洲天天影视| 欧美一级午夜免费电影| 在线播放日韩专区| 亚洲美女区一区| 国产精品专区h在线观看| 久久色中文字幕| 欧美大片免费观看| 亚洲永久免费观看| 久久久精品2019中文字幕神马| 亚洲国产精品久久久久婷婷884| 亚洲人成网站精品片在线观看| 欧美日韩一二区| 久久精品系列| 欧美精品手机在线| 久久九九99视频| 欧美日韩国产另类不卡| 久久久久99| 欧美日韩中字| 欧美~级网站不卡| 国产精品毛片a∨一区二区三区| 久久色中文字幕| 国产精品av一区二区| 欧美.www| 国产综合精品| 99国产精品久久久久久久| 激情成人av在线| 亚洲一区制服诱惑| av成人黄色| 看片网站欧美日韩| 久久精品在线| 国产精品国产三级国产普通话蜜臀 | 国产日韩欧美自拍| 亚洲精品乱码视频| 亚洲黄一区二区三区| 香蕉久久久久久久av网站| 亚洲免费观看在线观看| 美国十次了思思久久精品导航| 性高湖久久久久久久久| 欧美日韩免费观看一区三区 | 久久国产精品色婷婷| 欧美日本在线一区| 亚洲激情在线播放| 亚洲国产精品久久久久秋霞影院 | 欧美成人综合网站| 久久尤物视频| 国产一区二区三区在线观看精品| 亚洲天堂av电影| 中文亚洲欧美| 欧美日韩国产天堂| 亚洲精品乱码久久久久久日本蜜臀| 亚洲电影免费| 久久综合999| 欧美丰满高潮xxxx喷水动漫| 国产亚洲在线| 久久精品夜夜夜夜久久| 久久久亚洲成人| 激情伊人五月天久久综合| 久久国产精品久久久久久| 久久精品一区| 国产综合亚洲精品一区二| 欧美一区二区免费视频| 久久久天天操| 亚洲第一精品福利| 欧美α欧美αv大片| 91久久视频| 亚洲一区网站| 国产视频一区三区| 久久影院午夜论| 最近中文字幕日韩精品 | 亚洲欧美日韩精品一区二区| 国产精品看片资源| 欧美在线视频a| 欧美成人免费大片| 亚洲美女精品一区| 国产精品网站在线观看| 欧美一级视频| 欧美国产精品日韩| 亚洲资源在线观看| 国产在线欧美日韩| 欧美大片18| 亚洲男人的天堂在线| 久久综合伊人77777麻豆| 亚洲精品资源美女情侣酒店| 欧美午夜一区| 久久久九九九九| 日韩视频专区| 久久久久久一区二区| 亚洲乱码国产乱码精品精天堂| 国产精品分类| 久久一综合视频| 亚洲午夜久久久| 欧美寡妇偷汉性猛交| 亚洲欧美综合| 亚洲开发第一视频在线播放| 国产欧美日韩综合一区在线播放| 欧美福利视频在线观看| 亚洲精品久久久久久久久久久| 亚洲女女做受ⅹxx高潮| 另类激情亚洲| 亚洲在线视频观看| 亚洲欧洲一区二区在线播放 | 亚洲欧美在线网| 亚洲第一中文字幕| 久久国产精彩视频| 正在播放亚洲| 亚洲国产精品www| 国产欧美一区二区三区在线老狼 | 在线观看欧美一区| 国产美女精品免费电影| 欧美国产日韩二区| 久久久7777| 欧美一区二区三区播放老司机| 亚洲伦理网站| 亚洲国产你懂的| 麻豆亚洲精品| 久久久精品国产免费观看同学| 中文日韩欧美| a4yy欧美一区二区三区| 亚洲黄色免费网站| 亚洲福利视频二区| 狠狠色综合网| 伊人成人在线视频| 黑人一区二区| 国产在线视频不卡二| 国产精品在线看| 国产精品稀缺呦系列在线| 国产精品v欧美精品v日韩精品| 欧美精品精品一区| 欧美精品18+| 欧美日韩精品一区视频| 欧美精选一区| 欧美日韩国产片| 欧美午夜视频在线| 国产精品欧美久久| 国产精品少妇自拍| 国产美女精品视频| 国内精品久久久久伊人av| 激情综合久久| 亚洲黄色免费| 亚洲色图在线视频| 亚洲一区观看| 欧美主播一区二区三区| 久久偷窥视频| 欧美国产日韩一区| 亚洲人成网站色ww在线| 中文久久乱码一区二区| 亚洲欧美日韩一区在线观看| 欧美一区二区黄| 久久综合九色综合久99| 欧美理论电影在线播放| 国产精品wwwwww| 国产一区二区在线观看免费| 在线视频国内自拍亚洲视频| 亚洲美女色禁图| 亚洲欧美日产图| 久久亚洲色图| 亚洲精品专区| 午夜国产精品影院在线观看| 久久久久久九九九九| 欧美护士18xxxxhd| 国产精品入口麻豆原神| 激情亚洲网站| 亚洲一二三区精品| 久久五月激情| 一本大道久久a久久精二百| 欧美一区二区三区的| 欧美v日韩v国产v| 国产精品视频免费| 亚洲日本在线观看| 欧美伊人影院| 亚洲精品五月天| 久久精品国产亚洲a| 欧美日韩国产在线看| 国内外成人免费激情在线视频网站| 亚洲精品系列| 久久精品人人爽| 日韩视频在线观看国产| 久久久久久久久岛国免费| 欧美三级日本三级少妇99| 亚洲电影免费观看高清完整版在线观看| 亚洲美女黄网| 免费日韩av| 欧美一区二区三区在线| 欧美日韩一区高清| 亚洲人被黑人高潮完整版| 久久久久久久97| 亚洲婷婷在线| 欧美日韩在线观看一区二区|