NSURL扩展文件 #import <Foundation/Foundation.h>
@interface NSURL (NSURL_Category)
@end ——————————————
#import "NSURL+NSURL_Category.h"
#import <objc/message.h>
@implementation NSURL (NSURL_Category)
+(void)load{
/*这里是对象方法交换 需要注意 对象方法交换使用 class_getInstanceMethod 获取对象方法 类方法交换使用 class_getClassMethod 获取类方法 */Method murlw = class_getInstanceMethod([NSURL class], @selector(initWithString:));Method curl = class_getInstanceMethod([NSURL class], @selector(HJStringWithRUL:));/*交换函数*/method_exchangeImplementations(murlw, curl);}
-(instancetype)HJStringWithRUL:(NSString *)url {
/*防止死循环*/NSURL *urls = [[NSURL alloc]HJStringWithRUL:url];if (urls == nil) { NSLog(@"接口有问题");}return urls;}
@end -调用_______________________________________ #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad { [super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:@”www.baidu.com是的”];
NSLog(@”%@”,url);
}
@end
新闻热点
疑难解答