通過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~"); }
上面的情況則會報錯!