runtime交换函数method_exchangeImplementations

来源:互联网 发布:淘宝伴侣下载 编辑:程序博客网 时间:2024/05/21 10:49

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

0 0
原创粉丝点击