不多說(shuō)直接上代碼
@interface NSString (YXMethodExchange)
- (NSString *)yx_uppercaseString;
@end
@implementation NSString (YXMethodExchange)
- (NSString *)yx_uppercaseString{
NSLog(@"執(zhí)行uppercaseString之前");
//這里看似會(huì)遞歸,但實(shí)際并不會(huì),因?yàn)閷?shí)際調(diào)用的是uppercaseString方法
NSString *uppercaseString = [self yx_uppercaseString];
NSLog(@"執(zhí)行uppercaseString之后 獲取到大寫(xiě):%@",uppercaseString);
return uppercaseString;
}
@end
調(diào)用場(chǎng)景代碼
Method up = class_getInstanceMethod([NSString class], @selector(uppercaseString));
Method myUp = class_getInstanceMethod([NSString class], @selector(yx_uppercaseString));
method_exchangeImplementations(up, myUp);
[@"hellow" uppercaseString];
這樣就能在指定的方法調(diào)用前后加上輸出日志??梢院芊奖愕膶?duì)IOS中沒(méi)有暴露的方法進(jìn)行日志打印,方便調(diào)試。這就是黑盒調(diào)試。