以更改系统的URLWithString:方法为案例
(1).h文件里面声名自己创建的方法
+ (instancetype)XC_URLWithStr:(NSString *)str;//一加载就运行的方法
+ (void)load
{
//Method成员方法
//获取对象方法class_getInstanceSize
//获取类方法class_getClassMethod
Method URLWithStr=class_getClassMethod(self, @selector(URLWithString:));
Method XC_URLWithStr=class_getClassMethod(self, @selector(XC_URLWithStr:));
//交换方法
method_exchangeImplementations(URLWithStr, XC_URLWithStr);
}
//此方法已和系统的URLWithString:方法交换
+ (instancetype)XC_URLWithStr:(NSString *)str
{
NSURL *url = [NSURL XC_URLWithStr:str];
if (url == nil) {
NSLog(@"哥们为nill");
}
return url;
}
在ViewController里面
NSURL *url = [NSURL URLWithString:@"www.baidu.com/中文"];
打印结果为:哥们为nill
注意点:不需要再任何地方导入我们创建的继承于NSURL的分类方法