NSURL URLWithString:relativeToURL:剪切相对URL [英] NSURL URLWithString:relativeToURL: is clipping relative url
问题描述
我正在尝试使用RestKit实现iOS应用。在我到目前为止看到的所有示例中,以下代码用于创建URL:
I'm trying to implement an iOS app, which uses RestKit. In all examples I've seen so far the following code is used to create the URLs:
NSURL *baseURL = [NSURL URLWithString:@"https://api.service.com/v1"];
NSURL *relativeURL = [NSURL URLWithString:@"/files/search" relativeToURL:baseURL];
但是 [relativeURL absoluteString]
将返回 https://api.service.com/files/search
。
所以我尝试了几个例子:
So I tried a few examples:
NSURL *baseURL1 = [NSURL URLWithString:@"https://api.service.com/v1/"];
NSURL *baseURL2 = [NSURL URLWithString:@"https://api.service.com/v1"];
NSURL *baseURL3 = [NSURL URLWithString:@"/v1" relativeToURL:[NSURL URLWithString:@"https://api.service.com"]];
NSURL *relativeURL1 = [NSURL URLWithString:@"/files/search" relativeToURL:baseURL1];
NSURL *relativeURL2 = [NSURL URLWithString:@"/files/search" relativeToURL:baseURL2];
NSURL *relativeURL3 = [NSURL URLWithString:@"/files/search" relativeToURL:baseURL3];
NSURL *relativeURL4 = [NSURL URLWithString:@"files/search" relativeToURL:baseURL1];
NSURL *relativeURL5 = [NSURL URLWithString:@"files/search" relativeToURL:baseURL2];
NSURL *relativeURL6 = [NSURL URLWithString:@"files/search" relativeToURL:baseURL3];
NSLog(@"1: %@", [relativeURL1 absoluteString]);
NSLog(@"2: %@", [relativeURL2 absoluteString]);
NSLog(@"3: %@", [relativeURL3 absoluteString]);
NSLog(@"4: %@", [relativeURL4 absoluteString]);
NSLog(@"5: %@", [relativeURL5 absoluteString]);
NSLog(@"6: %@", [relativeURL6 absoluteString]);
这是输出:
1: https://api.service.com/files/search
2: https://api.service.com/files/search
3: https://api.service.com/files/search
4: https://api.service.com/v1/files/search
5: https://api.service.com/files/search
6: https://api.service.com/files/search
所以回归我想要的唯一例子是#4。任何人都可以解释原因吗?
So the only example returning what I want is #4. Can anyone explain why?
推荐答案
我读过 [RFC1808] 定义了解析相对URL的规范算法
I read [RFC1808] which defines the normative algorithm for resolving relative URLs
2期:
-
相对网址可能不以/开头,或者它被认为是绝对的:
the relative url may not start with a / or it is considered to be absolute:
Step 4: If the embedded URL path is preceded by a slash "/", the
path is not relative and we skip to Step 7."
当基本网址以非斜杠结束时。跳过上一个斜杠的所有内容
when the base url ends with a non-slash. everything from the last slash on is skipped
Step 6: The last segment of the base URL's path (anything
following the rightmost slash "/", or the entire path if no
slash is present) is removed and the embedded URL's path is
appended in its place. The following operations are
then applied, in order, to the new path:"
所以这解释了。 baseURL需要以/结尾,并且相对url不应以/
so that explains it. the baseURL needs to end in a / and the relative url shouldn't start with a /
这篇关于NSURL URLWithString:relativeToURL:剪切相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!