#import "ViewController.h" #import <objc/runtime.h> #import "NSString+EocMyAdditions.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //类的方法列表会把选择子的名称映射到相关的方法实现之上,使得“动态消息派发系统”可以据此找到应该调用的方法。这些方法均以函数指针的形式来表示,这种指针叫IMP id(*IMP)(id,SEL); //1.如何互换两个方法的实现 Method originaMethod = class_getInstanceMethod([NSString class], @selector(lowercaseString)); Method swappedMethod = class_getInstanceMethod([NSString class], @selector(eoc_myLowercaseString)); method_exchangeImplementations(originaMethod, swappedMethod);//方法的实现进行交换 就能够进行黑盒的调试 NSString *string = @"ThIs is tHe StriNg"; NSString *lowercaseString = [string lowercaseString]; NSLog(@"%@",lowercaseString); //this is the string } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <Foundation/Foundation.h> @interface NSString (EocMyAdditions) -(NSString *)eoc_myLowercaseString; @end #import "NSString+EocMyAdditions.h" @implementation NSString (EocMyAdditions) -(NSString *)eoc_myLowercaseString { NSString *lowercase = [self eoc_myLowercaseString];//用此方法跟lowercaseString互换,不会陷入递归的死循环 NSLog(@"分类中打印》》》》》》》》%@ =>%@",self,lowercase);////分类中打印》》》》》》》》ThIs is tHe StriNg =>this is the string return lowercase; } @end