Method Swizzling對Method的要求

        通過method_exchangeImplementation、class_replaceMethod、method_setImplementation進行Method Swizzling時,對originalMethod和destMethod的要求如下:

(1)返回值類型可以不同。如:

- (void)func1:(NSString *)word{
    NSLog(@"This is func1~");
    }
- (BOOL)func2:(NSString *)word{
    NSLog(@"This is func2~");
    return YES;
}

(2)參數個數可以不同。如:

- (void)func1:(NSString *)word{
    NSLog(@"This is func1~");
    }
- (void)func2:(NSString *)word sec:(NSString *)word2{
    NSLog(@"This is func2~");
}

(3)但是!對應參數類型必須相同!!如:

- (void)func1:(NSString *)word{
    NSLog(@"This is func1~");
    }
- (void)func2:(NSString *)word sec:(NSString *)word2{
    NSLog(@"This is func2~");
}

第一個參數都爲NSString類型。


- (void)func1:(NSString *)word sec:(int)word2 third:(BOOL)word3{
    NSLog(@"This is func1~");
    }
- (void)func2:(NSString *)word sec:(int)word2 {
    NSLog(@"This is func2~");
}

func1第一個參數爲NSString,func2第一個參數也爲NSString;func1第二參數爲int,func2第二個參數也爲int。


- (void)func1:(NSString *)word sec:(int)word2 third:(BOOL)word3{
    NSLog(@"This is func1~");
    }
- (void)func2:(NSString *)word sec:(int)word2 third:(NSString *)word3 {
    NSLog(@"This is func2~");
}

上面的情況則會報錯!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章