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

羅朝輝(飄飄白云)

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

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

在前文深入淺出Cocoa之消息中,我簡(jiǎn)要介紹了ObjC 中消息的基本情況,包括SEL查找,緩存以及消息轉(zhuǎn)發(fā)等。在本文中,我要介紹一個(gè)很有趣的技術(shù),Method swizzling,通過這個(gè)手法,我們可以動(dòng)態(tài)修改方法的實(shí)現(xiàn),從而達(dá)到修改類行為的目的。當(dāng)然,還有其他辦法(如 ClassPosing,Category)也可以達(dá)到這個(gè)目的。ClassPosing 是針對(duì)類級(jí)別的,是重量級(jí)的手法,Category 也差不多,比較重量級(jí),此外 Category 還無法避免下面的遞歸死循環(huán)(如果你的代碼出現(xiàn)了如下形式的遞歸調(diào)用,應(yīng)該考慮一下你的設(shè)計(jì),而不是使用在這里介紹的 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)和實(shí)例(instance)都是對(duì)象,類對(duì)象有自己的類方法列表,實(shí)例對(duì)象有自己的實(shí)例方法列表,這些方法列表(struct objc_method_list)是存儲(chǔ)在 struct objc_class 中的。每個(gè)方法列表存儲(chǔ)近似 SEL:Method 的對(duì),Method 是一個(gè)對(duì)象,包含方法的具體實(shí)現(xiàn) impl。由此可知,我們只需要修改 SEL 對(duì)應(yīng)的 Method 的 impl 既可以達(dá)到修改消息行為的目的。下面來看代碼:

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ū)分類方法和實(shí)例方法;
2,取得 SEL 對(duì)應(yīng)的 Method;
3,修改 Method 的 impl,在這里是通過交換實(shí)現(xiàn)的。

上面的代碼是可以工作的,但還不夠完善。Apple 10.5 提供了交換 Method 實(shí)現(xiàn)的 API: method_exchangeImplementations 。下面我們使用這個(gè)新 API,并以 NSObject category的形式給出新的實(shí)現(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://m.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)用的時(shí)序圖。


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

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

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

評(píng)論

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

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

多謝鼓勵(lì),一起加油~  回復(fù)  更多評(píng)論
  

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

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

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲一区999| 欧美xxx在线观看| 欧美午夜宅男影院在线观看| 久久久久久久性| 久久精品国产91精品亚洲| 午夜久久资源| 久久精品91| 美女图片一区二区| 免费日韩一区二区| 欧美成人国产| 久久人人97超碰人人澡爱香蕉| 亚洲欧美成人| 9久草视频在线视频精品| 亚洲免费成人| 亚洲午夜一区二区三区| 亚洲在线免费| 久久夜色精品国产| 欧美日韩福利视频| 欧美视频官网| 韩国三级在线一区| 日韩网站在线| 欧美一二三区在线观看| 久久久久久久综合色一本| 欧美成人精品不卡视频在线观看| 欧美激情亚洲综合一区| 中文av字幕一区| 国产精品国产一区二区| 国产欧美日韩一区二区三区| 激情欧美一区二区三区| 亚洲精品五月天| 欧美亚洲免费高清在线观看| 久久蜜桃资源一区二区老牛| 亚洲精品一区二区三区四区高清| 亚洲免费在线视频| 免费亚洲网站| 国产亚洲永久域名| 一区二区三区偷拍| 美女久久一区| 日韩亚洲欧美高清| 久久久国产一区二区| 欧美日韩一二三四五区| 好男人免费精品视频| 在线亚洲伦理| 欧美成人激情视频| 欧美一站二站| 国产精品日韩在线观看| 亚洲欧洲日本国产| 久久亚洲综合色| 中国成人在线视频| 欧美成人午夜剧场免费观看| 国产一区二区精品久久91| 亚洲午夜女主播在线直播| 欧美成人激情视频免费观看| 欧美一级久久久| 国产精品区一区| 一本色道久久88综合亚洲精品ⅰ| 久久综合九色综合久99| 午夜免费电影一区在线观看| 欧美日韩在线视频首页| 一区精品在线播放| 欧美专区在线播放| 亚洲午夜久久久久久久久电影院| 你懂的视频欧美| 亚洲高清在线视频| 日韩午夜免费| 久久精品1区| 红桃视频国产一区| 欧美中文在线免费| 中文久久乱码一区二区| 国产精品进线69影院| 亚洲美女在线观看| 亚洲人成在线观看网站高清| 欧美在线免费观看视频| 激情欧美一区二区三区在线观看| 久久国产精品亚洲va麻豆| 亚洲欧美一区二区三区久久| 国产精品资源在线观看| 欧美一级视频精品观看| 亚洲一区在线视频| 国产日产欧美精品| 久久久久九九九| 久久午夜精品一区二区| 亚洲激情黄色| 乱人伦精品视频在线观看| 亚洲免费影视第一页| 国产精品视频成人| 欧美在线观看一区二区三区| 欧美一区二区三区四区在线 | 亚洲欧美日韩另类| 国精品一区二区| 欧美国产日韩一区二区三区| 欧美黄色成人网| 亚洲欧美视频在线观看| 欧美在线影院| 亚洲国产乱码最新视频 | 欧美一级理论片| 尤物网精品视频| 亚洲靠逼com| 国产一区二区看久久| 美女主播视频一区| 欧美精品一线| 欧美在线观看视频一区二区| 久久久久久亚洲精品杨幂换脸| 亚洲二区在线观看| 在线综合亚洲欧美在线视频| 伊人久久大香线| 日韩一级视频免费观看在线| 国产亚洲精品aa| 亚洲日本欧美在线| 国产综合18久久久久久| 亚洲精品在线看| 亚洲国产欧美国产综合一区| 亚洲欧美www| 日韩亚洲精品在线| 久久精品成人一区二区三区| 亚洲视频在线观看一区| 理论片一区二区在线| 午夜国产一区| 欧美日韩久久不卡| 亚洲电影免费观看高清完整版在线观看| 欧美色综合网| 亚洲国产成人久久综合| 狠狠色综合网站久久久久久久| 夜夜嗨av一区二区三区四区| 亚洲福利视频二区| 久久av一区二区三区漫画| 亚洲一区观看| 欧美精品久久久久久| 免费不卡视频| 韩国av一区| 久久9热精品视频| 午夜精品视频| 欧美日韩一区二区免费视频| 欧美国产激情| 在线观看亚洲a| 亚洲综合二区| 亚洲欧美日韩在线播放| 欧美激情精品久久久久久| 欧美成人免费小视频| 国内久久视频| 欧美中文在线免费| 欧美中文字幕久久| 国产精品视频xxxx| 香蕉国产精品偷在线观看不卡| 亚洲午夜日本在线观看| 欧美日本二区| 亚洲国产岛国毛片在线| 欧美亚洲日本网站| 亚洲美女区一区| 欧美成人黑人xx视频免费观看| 蜜乳av另类精品一区二区| 激情亚洲网站| 久久久久久伊人| 欧美99久久| 亚洲第一色中文字幕| 欧美成人午夜| 一区二区三区四区五区在线| 亚洲综合成人在线| 国产精品嫩草影院av蜜臀| 亚洲激情另类| 在线视频日本亚洲性| 欧美日韩综合一区| 亚洲欧美一区二区三区极速播放| 久久福利毛片| 在线观看视频日韩| 欧美精品123区| 一区二区三区福利| 久久精品视频在线| 亚洲第一区在线| 欧美美女喷水视频| 亚洲一区日本| 欧美xxx在线观看| 亚洲午夜久久久久久久久电影网| 欧美午夜片欧美片在线观看| 欧美亚洲免费高清在线观看| 欧美成人午夜激情| 在线一区二区三区四区| 国产精品男女猛烈高潮激情| 久久免费99精品久久久久久| 亚洲国产视频直播| 性欧美1819性猛交| 影音先锋另类| 国产精品扒开腿爽爽爽视频| 久久久www成人免费精品| 亚洲国产精品一区二区三区| 亚洲欧美日韩在线不卡| 尤物精品在线| 国产精品美女主播| 久久久久久一区| 亚洲永久免费av| 亚洲黄网站黄| 久久精品日韩一区二区三区| 一个色综合av| 亚洲成人在线观看视频| 国产精品免费看久久久香蕉| 欧美韩国在线| 久久久久久**毛片大全| 亚洲天堂av在线免费观看| 亚洲国产成人午夜在线一区| 久久精品人人做人人爽电影蜜月|