cocoa之高级开发RunTime高级API

1、系统类的方法实现部分替换

    Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString));

    Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString));

    method_exchangeImplementations(m1, m2);

    log list:  

(lldb)po [@"assAAAAss" lowercaseString]

ASSAAAASS

(lldb) po [@"sssKAAAss" uppercaseString]

ssskaaass

2、自定义类的方法实现部分替换

在这里要说到一个黑马的典型死循环(人称自己++自己)

题目:- (void)viewDidLoad {

[self methodSetImplementation];

[self test];

-(void)test{ 

[self test];

}

如题:- (void) methodSetImplementation { 

Method method = class_getInstanceMethod([ViewController class],@selector(justLog1));

IMP originalImp = method_getImplementation(method);

Method m1 = class_getInstanceMethod([ViewController class],@selector(test)); 

method_setImplementation(m1, originalImp);

}

- (void) justLo


你可能感兴趣的:(cocoa之高级开发RunTime高级API)