URLWithString: relativeToURL: 的

2019-05-22  本文已影响0人  宇轩Simid

此方法最常见的就是处理网络http的时候用到,尤其是在AFNetWoring中使用到。

作用是把baseUrl和urlPath进行自动拼接的一个方法。

正确操作:
例子:
baseUrl:http://www.baidu.com/
urlPath: aaa/bbb/ccc/info?key1=dadf&key2=......

拼接出来是这样的:
http://www.baidu.com/aaa/bbb/ccc/info?key1=dadf&key2=......

出问题的操作:
例子:
baseUrl:http://www.baidu.com/kkkk
urlPath: aaa/bbb/ccc/info?key1=dadf&key2=......

拼接出来是这样的:
http://www.baidu.com/aaa/bbb/ccc/info?key1=dadf&key2=......

其中baseUrl中的kkkk丢失了。

处理方法:
在判断baseUrl后加上“/”就可以解决了,或者在后面拼接一个空字符串也是可以的。
url = [url URLByAppendingPathComponent:@""];

AFNetWorking中的处理方法:
if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
url = [url URLByAppendingPathComponent:@""];
}

上一篇下一篇

猜你喜欢

热点阅读