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

羅朝輝(飄飄白云)

關注嵌入式操作系統,移動平臺,圖形開發。-->加微博 ^_^

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

在前文深入淺出Cocoa之消息中,我簡要介紹了ObjC 中消息的基本情況,包括SEL查找,緩存以及消息轉發等。在本文中,我要介紹一個很有趣的技術,Method swizzling,通過這個手法,我們可以動態修改方法的實現,從而達到修改類行為的目的。當然,還有其他辦法(如 ClassPosing,Category)也可以達到這個目的。ClassPosing 是針對類級別的,是重量級的手法,Category 也差不多,比較重量級,此外 Category 還無法避免下面的遞歸死循環(如果你的代碼出現了如下形式的遞歸調用,應該考慮一下你的設計,而不是使用在這里介紹的 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 是一個對象,包含方法的具體實現 impl。由此可知,我們只需要修改 SEL 對應的 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,首先,區分類方法和實例方法;
2,取得 SEL 對應的 Method;
3,修改 Method 的 impl,在這里是通過交換實現的。

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

#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;
}

輸出結果為下表。注意,test 3 中調用了遞歸調用“自己”的方法,你能理解為什么沒有出現死循環么?
========= 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 解釋:在函數體 {} 之間的部分是真正的 IMP,而在這之前的是 SEL。通常情況下,SEL 是與 IMP 匹配的,但在 swizzling 之后,情況就不同了。下圖就是調用的時序圖。


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

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

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

評論

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

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

多謝鼓勵,一起加油~  回復  更多評論
  

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

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

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美精品一卡| 亚洲一区在线观看视频 | 亚洲自拍电影| 麻豆国产精品va在线观看不卡 | 免费人成精品欧美精品| 亚洲色图综合久久| 欧美精品久久久久久| 极品av少妇一区二区| 亚洲在线一区二区三区| 亚洲高清中文字幕| 亚洲在线免费视频| 欧美日韩亚洲在线| 亚洲激情国产| 久久青青草综合| 亚洲私人影吧| 欧美日韩麻豆| 亚洲毛片av| 欧美成人午夜激情在线| 久久精品成人一区二区三区蜜臀 | 亚洲经典三级| 久久久人人人| 午夜精品三级视频福利| 国产精品国产三级国产a| 日韩亚洲国产欧美| 亚洲国产成人一区| 欧美1区视频| 亚洲高清不卡| 免费在线欧美黄色| 久久久久久久一区| 国产日韩欧美一区| 欧美一区在线视频| 亚洲在线观看免费视频| 一本色道婷婷久久欧美| 亚洲欧美日韩精品久久| 欧美日韩亚洲高清一区二区| 亚洲日本欧美天堂| 欧美激情1区2区| 久久综合九色九九| 在线观看一区视频| 蜜桃久久av一区| 久久久国产91| 精品91在线| 另类成人小视频在线| 久久高清福利视频| 一区在线观看| 欧美成人xxx| 久久尤物视频| 亚洲电影免费在线观看| 蜜桃av噜噜一区| 久热re这里精品视频在线6| 在线观看91精品国产入口| 美女成人午夜| 蜜桃精品久久久久久久免费影院| 亚洲第一色在线| 欧美国产综合一区二区| 欧美xx69| 一本色道久久综合亚洲精品高清 | 欧美va天堂在线| 亚洲区在线播放| 亚洲国产99精品国自产| 欧美精品日韩一本| 亚洲一区国产精品| 亚洲在线视频| 日韩午夜电影av| 亚洲日本va午夜在线影院| 欧美精品系列| 亚洲天堂视频在线观看| 亚洲性人人天天夜夜摸| 国产午夜亚洲精品羞羞网站| 久久亚洲不卡| 欧美激情亚洲自拍| 亚洲综合清纯丝袜自拍| 午夜在线不卡| 亚洲大片在线| 亚洲精品裸体| 国产精品色网| 六月婷婷久久| 欧美精品一区二区三区视频| 一本久久综合亚洲鲁鲁五月天| 亚洲午夜性刺激影院| 国产在线欧美日韩| 亚洲高清色综合| 欧美日韩在线观看视频| 欧美一级电影久久| 久久久久久久久久久久久女国产乱 | 欧美诱惑福利视频| 久久久久久尹人网香蕉| 亚洲美女在线看| 亚洲午夜精品福利| 在线观看国产一区二区| 亚洲精品一区在线观看| 国产农村妇女毛片精品久久麻豆 | 美女脱光内衣内裤视频久久影院 | 国产精品伦子伦免费视频| 久久xxxx精品视频| 另类成人小视频在线| 日韩一二三在线视频播| 亚洲在线网站| 亚洲国产精品久久久久秋霞蜜臀| 亚洲美洲欧洲综合国产一区| 国产精品专区一| 欧美激情精品久久久久久免费印度 | 亚洲精品日韩激情在线电影| 在线亚洲精品| 亚洲高清视频在线观看| 国产精品99久久久久久有的能看| 在线看片成人| 一本一本久久a久久精品综合妖精| 国产亚洲精品久久飘花| 亚洲国产精品久久人人爱蜜臀| 国产精品美女久久久久久2018 | 美日韩免费视频| 亚洲欧美一区二区三区极速播放| 久久久一区二区| 亚洲欧美日韩另类| 免费毛片一区二区三区久久久| 午夜欧美精品久久久久久久| 欧美成人精品三级在线观看| 久久国产乱子精品免费女| 欧美激情一区二区久久久| 久久久国产精品一区二区三区| 欧美日韩国产在线一区| 免费成人美女女| 国产精品午夜春色av| 亚洲激情专区| 在线免费日韩片| 亚洲欧美在线高清| 中文在线一区| 免费欧美网站| 久久午夜视频| 国产精品入口夜色视频大尺度| 亚洲精品视频免费观看| 亚洲国产精品一区二区第四页av| 欧美一级片久久久久久久 | 一区二区三区国产精品| 亚洲日本在线观看| 久久久噜噜噜| 久久激情五月丁香伊人| 国产精品成人一区二区三区吃奶 | 久久综合伊人77777尤物| 国产精品久久久久秋霞鲁丝| 最新高清无码专区| 亚洲电影自拍| 久久久精品2019中文字幕神马| 亚久久调教视频| 欧美四级电影网站| 亚洲欧洲在线播放| 91久久精品一区二区别| 久久久久成人精品| 久久久.com| 国产美女一区| 亚洲自拍都市欧美小说| 亚洲综合国产精品| 欧美午夜宅男影院| 99国产成+人+综合+亚洲欧美| 亚洲三级免费| 欧美aⅴ一区二区三区视频| 欧美 亚欧 日韩视频在线| 国产午夜久久| 性色一区二区三区| 欧美一区午夜精品| 国产视频丨精品|在线观看| 亚洲午夜激情免费视频| 亚洲伊人伊色伊影伊综合网| 欧美日韩一区视频| 99在线精品视频| 亚洲午夜激情网站| 欧美日韩精品免费观看视频| 亚洲精品中文字幕在线| av成人免费观看| 欧美人与性动交α欧美精品济南到 | 国产精品久久久久aaaa樱花| 夜夜嗨一区二区| 亚洲性视频网站| 国产精品扒开腿做爽爽爽视频 | 一区二区在线观看视频| 久久精品中文字幕免费mv| 久久天天综合| 在线观看欧美日韩国产| 久久亚洲国产精品一区二区| 蜜桃av一区二区| 亚洲经典三级| 欧美日本亚洲韩国国产| 99re6热只有精品免费观看| 亚洲一品av免费观看| 国产精品久久77777| 亚洲在线免费| 久久一区二区三区四区五区| 伊人久久婷婷| 欧美电影资源| 99国产精品99久久久久久| 亚洲一区视频在线| 国产欧美午夜| 久久久久久久波多野高潮日日| 亚洲第一色中文字幕| 一区二区三区免费在线观看| 国产精品激情电影| 校园春色国产精品| 免费在线成人av| 一区二区三区日韩|