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

羅朝輝(飄飄白云)

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

  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  85 隨筆 :: 0 文章 :: 169 評(píng)論 :: 0 Trackbacks
[Cocoa]深入淺出Cocoa之 Method Swizzling
羅朝輝(http://www.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://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)用的時(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 羅朝輝 閱讀(2552) 評(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>
            亚洲美女电影在线| 欧美一区二区三区视频免费| 一区二区欧美在线| 欧美国产日韩免费| 亚洲国产成人一区| 欧美精品免费播放| 一本色道久久88综合亚洲精品ⅰ| 亚洲一区二区精品在线| 国产精品免费看| 欧美在线黄色| 欧美 日韩 国产精品免费观看| 国产精品视频| 久久精品国产免费观看| 亚洲电影在线免费观看| 99热精品在线观看| 国产精品欧美激情| 久久久久久综合| 99热免费精品| 噜噜爱69成人精品| 一区二区欧美日韩视频| 国产视频亚洲精品| 欧美国产视频在线| 午夜久久久久久| 亚洲国产高潮在线观看| 亚洲欧美日本在线| 亚洲国产精品第一区二区| 欧美色另类天堂2015| 久久精品国产999大香线蕉| 日韩视频免费在线| 亚洲卡通欧美制服中文| 国产精品日韩在线一区| 蜜桃av噜噜一区| 亚洲欧美日本伦理| 亚洲电影在线观看| 久久久99久久精品女同性| 99在线精品观看| 在线看日韩欧美| 国产精品久久久久免费a∨| 欧美风情在线观看| 久久久久久久网| 亚洲亚洲精品在线观看| 最新亚洲视频| 老司机免费视频一区二区三区| 国产精品99久久久久久www| 精品成人在线视频| 国产精品视频免费一区| 欧美福利一区二区三区| 久久久久久久尹人综合网亚洲| 亚洲制服丝袜在线| 亚洲靠逼com| 亚洲高清123| 欧美成人有码| 美日韩丰满少妇在线观看| 香蕉久久国产| 亚洲欧美激情四射在线日 | 亚洲免费观看在线观看| 久久综合色播五月| 久久国产乱子精品免费女 | 欧美美女福利视频| 巨胸喷奶水www久久久免费动漫| 午夜精品在线| 午夜精品av| 亚洲欧美激情视频| 亚洲一区欧美二区| 一区二区三区四区五区精品| 亚洲免费观看高清完整版在线观看熊 | 玖玖视频精品| 欧美在线一二三四区| 亚洲欧美日韩在线高清直播| 亚洲一区二区久久| 亚洲欧美久久| 欧美亚洲综合网| 香蕉成人啪国产精品视频综合网| 亚洲女ⅴideoshd黑人| 亚洲综合成人在线| 亚洲欧美精品在线| 欧美一级淫片aaaaaaa视频| 欧美一级大片在线观看| 欧美一级欧美一级在线播放| 欧美影院视频| 久久艳片www.17c.com| 久久尤物电影视频在线观看| 美女国内精品自产拍在线播放| 久久综合九色综合欧美狠狠| 久久永久免费| 亚洲黄色尤物视频| 亚洲人成人一区二区在线观看| 亚洲精品日韩久久| 在线一区二区三区四区| 亚洲欧美日韩在线高清直播| 欧美在线看片| 女同性一区二区三区人了人一| 欧美国产一区二区在线观看 | 精品999日本| 亚洲日本在线观看| 国产亚洲欧美日韩精品| 国产在线播放一区二区三区 | 日韩性生活视频| 亚洲视频中文| 久久精品国产99| 欧美大秀在线观看| 一区二区高清视频| 午夜免费电影一区在线观看| 久热精品视频在线观看一区| 欧美精品激情| 国产一区二区三区免费不卡 | 一区二区三区四区五区在线| 性做久久久久久久久| 欧美成人午夜免费视在线看片 | 中文av一区特黄| 久久成人免费电影| 亚洲高清不卡| 亚洲综合视频网| 蜜臀av国产精品久久久久| 国产精品v欧美精品∨日韩| 国产原创一区二区| 亚洲天堂成人在线视频| 老牛嫩草一区二区三区日本| 亚洲狼人综合| 久久免费偷拍视频| 国产精品入口夜色视频大尺度| 亚洲高清不卡| 久久国产手机看片| 亚洲人成艺术| 久久久久中文| 国产精品视频| 一本色道久久综合精品竹菊 | 亚洲国产婷婷香蕉久久久久久99| 亚洲午夜精品网| 欧美va亚洲va香蕉在线| 国产喷白浆一区二区三区| 日韩视频精品| 欧美91精品| 午夜视频久久久| 欧美视频一区| 亚洲精品视频一区二区三区| 欧美在线综合| 亚洲免费成人| 欧美黄色一级视频| 一区精品在线播放| 欧美专区在线播放| 一本色道久久综合亚洲精品高清| 欧美成人免费一级人片100| 国产真实乱偷精品视频免| 亚洲一区二区三区免费视频| 亚洲高清不卡| 玖玖综合伊人| 在线看欧美视频| 久久先锋影音| 销魂美女一区二区三区视频在线| 欧美性久久久| 一区二区三区日韩精品| 亚洲国产精品久久| 鲁大师成人一区二区三区| 影音先锋亚洲视频| 久久男人av资源网站| 香蕉av福利精品导航| 国产精品一区二区久久久| 亚洲伊人伊色伊影伊综合网| 日韩一级精品视频在线观看| 欧美伦理a级免费电影| 99精品欧美| 日韩图片一区| 欧美三级视频在线播放| 亚洲一区免费看| 一本色道久久综合亚洲91| 欧美日韩国产页| 中文精品一区二区三区| 夜夜嗨av一区二区三区四季av| 欧美全黄视频| 亚洲伊人伊色伊影伊综合网| 亚洲欧美福利一区二区| 国产精品女主播| 久久久久国产成人精品亚洲午夜| 欧美资源在线| 亚洲成人在线观看视频| 亚洲成人直播| 欧美日韩亚洲精品内裤| 午夜欧美大片免费观看| 亚洲女ⅴideoshd黑人| 国产午夜精品一区二区三区视频 | 欧美日本三区| 亚洲欧美国产高清va在线播| 小处雏高清一区二区三区 | 国产精品美女主播| 久久久久久久一区二区| 玖玖在线精品| 一区二区三区日韩欧美| 亚洲一区二区三区高清| 狠狠色丁香婷婷综合影院| 欧美肥婆在线| 欧美视频一区二区三区…| 久久国产一区二区三区| 开心色5月久久精品| 一本到高清视频免费精品| 在线亚洲+欧美+日本专区| 国内精品嫩模av私拍在线观看| 欧美激情精品久久久六区热门| 欧美三级第一页| 久久亚洲午夜电影|