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
网友评论