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

羅朝輝(飄飄白云)

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

  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 羅朝輝 閱讀(2543) 評論(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>
            欧美视频一区在线观看| 一区二区三区国产盗摄| 一区二区三区免费观看| 黑人中文字幕一区二区三区| 一二三区精品| 亚洲国产美国国产综合一区二区| 亚洲一区图片| 亚洲字幕一区二区| 欧美精品一区二区三区蜜桃| 欧美成人四级电影| 狠狠色狠狠色综合系列| 亚洲欧美日韩在线不卡| 亚洲一级影院| 欧美三级免费| 99视频精品| 国产精品99久久不卡二区| 欧美激情亚洲另类| 亚洲激情网站免费观看| 亚洲激情在线激情| 蜜桃av综合| 亚洲电影免费观看高清完整版在线观看 | 久久激情五月丁香伊人| 欧美在线视频免费观看| 国产色产综合色产在线视频| 亚洲香蕉成视频在线观看 | 久久精品国产精品亚洲| 国产精品久久久久久户外露出 | 亚洲国产另类 国产精品国产免费| 亚洲大片av| 久久人人精品| 免费看的黄色欧美网站| 亚洲国产婷婷香蕉久久久久久99| 老司机精品视频网站| 欧美暴力喷水在线| 亚洲日本一区二区三区| 欧美日韩高清在线| 亚洲无限av看| 欧美在线在线| 伊人久久大香线| 欧美 日韩 国产一区二区在线视频| 欧美激情第10页| 一区二区电影免费观看| 国产精品福利av| 欧美一区二区日韩| 欧美 日韩 国产 一区| 亚洲精品一区二区三区四区高清 | 久久久蜜臀国产一区二区| 黄色欧美成人| 欧美日韩91| 亚洲小视频在线观看| 久久露脸国产精品| 亚洲精品久久久一区二区三区| 欧美日韩国产色综合一二三四 | 国产视频在线观看一区| 猛干欧美女孩| 亚洲午夜av在线| 开心色5月久久精品| 亚洲麻豆国产自偷在线| 国产欧美精品| 欧美gay视频| 亚洲综合成人在线| 欧美电影免费观看| 性久久久久久久久| 亚洲片在线观看| 国产精品婷婷午夜在线观看| 久久综合色天天久久综合图片| 日韩午夜在线视频| 美女在线一区二区| 亚洲制服丝袜在线| 亚洲黄色有码视频| 国产日韩欧美综合| 欧美日韩亚洲激情| 麻豆精品视频在线| 亚洲尤物在线视频观看| 亚洲第一在线| 久久嫩草精品久久久精品一| 亚洲一区成人| 亚洲精品一区二区三区99| 国产一区二区三区黄| 欧美亚男人的天堂| 欧美高清视频www夜色资源网| 欧美在线视频免费观看| 在线视频日本亚洲性| 91久久精品美女| 你懂的国产精品| 久久精品国产欧美亚洲人人爽| 亚洲精品孕妇| 亚洲激情视频| 亚洲第一久久影院| 国产亚洲精品7777| 国产欧美精品在线观看| 国产精品扒开腿做爽爽爽视频| 欧美成人黑人xx视频免费观看| 久久精品91| 欧美一区二区免费视频| 亚洲欧美影院| 亚洲欧美国产另类| 亚洲欧美日本另类| 亚洲免费视频成人| 亚洲无亚洲人成网站77777| 一本色道久久综合狠狠躁篇的优点| 亚洲电影免费观看高清完整版在线观看| 久久婷婷影院| 久久夜色精品国产欧美乱极品| 久久国产精彩视频| 久久国产天堂福利天堂| 欧美一区二区视频免费观看| 亚洲欧美一区二区视频| 香蕉成人伊视频在线观看| 亚洲自拍偷拍一区| 午夜伦理片一区| 欧美一级播放| 久久久久久91香蕉国产| 久久久久久69| 欧美gay视频| 欧美激情视频一区二区三区不卡| 欧美国产欧美亚洲国产日韩mv天天看完整| 久久综合久久美利坚合众国| 裸体一区二区| 亚洲国产另类 国产精品国产免费| 欧美激情一二三区| 亚洲精品乱码久久久久久蜜桃91 | 亚洲一区二区三区乱码aⅴ| 亚洲自拍高清| 久久久国产精品一区二区中文| 久久国产主播精品| 女女同性精品视频| 欧美日韩亚洲一区二区三区| 国产精品日韩一区二区| 国产专区欧美专区| 亚洲精品一区在线观看| 亚洲午夜av在线| 久久久久久久高潮| 欧美激情久久久久久| 日韩一级片网址| 午夜久久黄色| 免费观看日韩| 国产精品视频久久一区| 一区二区视频免费在线观看| 亚洲美女av黄| 久久国产精品99国产精| 欧美承认网站| 亚洲小少妇裸体bbw| 久久久久久网址| 国产精品a久久久久| 极品av少妇一区二区| 日韩一区二区精品| 久久久国产午夜精品| 亚洲精品一区久久久久久| 午夜精品免费在线| 欧美精品成人在线| 国产真实久久| 亚洲一区二区三区免费视频| 久久人人97超碰精品888 | 亚洲一区二区三区精品动漫| 久久久五月天| 国产精品一区三区| 亚洲经典自拍| 久久久亚洲精品一区二区三区| 亚洲激情在线观看| 久久久999精品| 国产精品爽黄69| 99精品国产在热久久下载| 久久人人九九| 亚洲在线成人| 欧美日韩国产精品一区| 在线日韩av| 久久综合久久综合久久| 亚洲午夜小视频| 欧美精品在线免费播放| 亚洲国产精品久久久久久女王| 欧美伊久线香蕉线新在线| 日韩亚洲欧美成人一区| 免费久久99精品国产自在现线| 国产亚洲美州欧州综合国| 亚洲欧美日韩国产精品| 亚洲精品乱码久久久久久蜜桃91 | 国产一区二区三区久久久久久久久 | 亚洲欧洲一区| 欧美成人精品h版在线观看| 午夜精品久久久| 国产精品日韩一区二区三区| 亚洲一区二区三区在线播放| 亚洲精品在线视频观看| 欧美成人性网| 亚洲精品自在久久| 亚洲高清在线观看| 欧美成人精品影院| 亚洲精品三级| 91久久亚洲| 欧美女人交a| 国产精品99久久99久久久二8| 亚洲毛片av| 欧美日韩中文字幕在线视频| 一本色道久久加勒比精品| 亚洲日本中文字幕| 欧美日韩无遮挡| 亚洲欧美卡通另类91av| 亚洲自拍高清| 黄色资源网久久资源365|