天天看點

Method Swizzling替換 NSURL 的initWithString: 奔潰

      我已經成功的替換了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