交换IMP

作者: Code_人生 | 来源:发表于2019-10-17 10:18 被阅读0次
        NSURL *url = [NSURL URLWithString:@"www.baidu.com"];
        NSURL *url1 = [NSURL URLWithString:@"www.baidu.com/中文"];
        NSLog(@"%@",url);
        NSLog(@"%@",url1);
    
    2019-10-17 10:03:41.407613+0800 MSDemoDYZ[10905:2932928] www.baidu.com
    2019-10-17 10:03:41.407685+0800 MSDemoDYZ[10905:2932928] (null)
    
    • 建一个NSURL的分类,在load方法中交换IMP
    #import "NSURL+DYZURL.h"
    #import <objc/runtime.h>
    
    
    @implementation NSURL (DYZURL)
    
    + (void)load {
        Method URLWithStr = class_getClassMethod(self, @selector(URLWithString:));
        Method DYZURLWithStr = class_getClassMethod(self, @selector(DYZ_URLWithString:));
        //交换?  SEL -- IMP
        method_exchangeImplementations(URLWithStr, DYZURLWithStr);
    }
    
    + (instancetype)DYZ_URLWithString:(NSString *)URLString {
        //我也不知道如何通过一个字符串创建一个url!!!!
        NSURL *url = [NSURL DYZ_URLWithString:URLString];
        if (url == nil) {
            URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        }
        return [NSURL DYZ_URLWithString:URLString];
    }
    
    
    @end
    
    2019-10-17 10:14:19.008203+0800 MSDemoDYZ[10943:2936864] www.baidu.com
    2019-10-17 10:14:19.008289+0800 MSDemoDYZ[10943:2936864] www.baidu.com/%E4%B8%AD%E6%96%87
    

    相关文章

      网友评论

          本文标题:交换IMP

          本文链接:https://www.haomeiwen.com/subject/lihmmctx.html