我們可以利用這個功能,去 "hook" 一些 API,修改其運作方式,對於新手來說這個功能是很危險的,但對於清楚 API 運作的高手來說,這個功能很有用,可以做一些 API 本身沒有的功能。
ExchangeImp.h
@interface NSObject (ExchangeImp) + (BOOL)exchangeMethod:(SEL)origSelector withMethod:(SEL)newSelector; @end
ExchangeImp.m
#import "ExchangeImp.h"
@implementation NSObject (ExchangeImp)
+ (BOOL)exchangeMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
Method orgMethod = class_getInstanceMethod(self, orgSelector);
Method newMethod = class_getInstanceMethod(self, newSelector);
if ((orgMethod != NULL) && (newMethod != NULL))
{
if (class_addMethod(self, orgSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
{
class_replaceMethod(self, newSelector, method_getImplementation(orgMethod), method_getTypeEncoding(orgMethod));
}
else
{
method_exchangeImplementations(origMethod, newMethod);
}
return YES;
}
return NO;
}
@end
沒有留言:
張貼留言