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
新聞熱點
疑難解答