我已经成功的替换了NSURL 的类方法URLWithString: 那就说明NSURL是支持Method Swizzling的,但是替换initWithString:却不成功,总是在url被释放的时候奔溃,报的是一个野指针的错误Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)。求大神解答。
代码如下:
#import "NSURL+MethodSwizzing.h"
#import <objc/runtime.h>
@implementation NSURL (QQNSURLMethodSwizzing)
+ (void)load {
Method system_Method = class_getClassMethod([self class], @selector(URLWithString:));
Method my_Method = class_getClassMethod([self class], @selector(URLWithTESTDifferentEnviromentString:));
method_exchangeImplementations(system_Method, my_Method);
Method systemInitMethod = class_getInstanceMethod([self class], @selector(initWithString:));
Method myMethod = class_getInstanceMethod([self class], @selector(initWithDifferentEnviromentString:));
method_exchangeImplementations(systemInitMethod, myMethod);
}
+ (NSURL *)URLWithTESTDifferentEnviromentString:(NSString *)urlString {
return [self URLWithTESTDifferentEnviromentString:urlString];
}
- (void)initWithDifferentEnviromentString:(NSString *)urlString {
[self initWithDifferentEnviromentString:urlString];
}
@end