Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UrlByAppendingPathComponent与UrlByAppendingPathExtension_Objective C_Ios_Ios5 - Fatal编程技术网

Objective c UrlByAppendingPathComponent与UrlByAppendingPathExtension

Objective c UrlByAppendingPathComponent与UrlByAppendingPathExtension,objective-c,ios,ios5,Objective C,Ios,Ios5,我已经阅读了文档,看起来有些边缘情况可能不同(尾部斜杠等),但我不清楚这两种方法之间的主要区别是什么。“组件”和“扩展”这两个术语在URL世界中是否有我以外的人理解的特殊含义?路径扩展用于向URL添加.html之类的内容,而路径组件用于添加/news/local之类的内容。路径扩展的文档: 如果原始URL以一个或多个正斜杠结尾,则这些正斜杠将从返回的URL中删除。在新URL的两部分之间插入句点 所以http://hello.com/news/将变成http://hello.com/news.ht

我已经阅读了文档,看起来有些边缘情况可能不同(尾部斜杠等),但我不清楚这两种方法之间的主要区别是什么。“组件”和“扩展”这两个术语在URL世界中是否有我以外的人理解的特殊含义?

路径扩展用于向URL添加
.html
之类的内容,而路径组件用于添加
/news/local
之类的内容。路径扩展的文档:

如果原始URL以一个或多个正斜杠结尾,则这些正斜杠将从返回的URL中删除。在新URL的两部分之间插入句点

所以
http://hello.com/news/
将变成
http://hello.com/news.html

路径组件的文档:

如果原始URL不以正斜杠结尾,并且pathComponent不以正斜杠开头,则会在返回URL的两部分之间插入正斜杠,除非原始URL是空字符串

所以
http://hello.com/news/
将变成
http://hello.com/news/html

下面是一个快速测试:

NSURL *originalURL = [NSURL URLWithString:@"http://hello.com/news"];
NSLog(@"%@", [originalURL URLByAppendingPathComponent:@"local"]);
NSLog(@"%@", [originalURL URLByAppendingPathExtension:@"local"]);
输出:

http://hello.com/news/local
http://hello.com/news.local

每当我对这样的事情有疑问,并且文档没有帮助,我就用逻辑测试来测试它

NSURL *baseURL = [NSURL URLWithString:@"http://foo.com/bar/baz"];
NSURL *appendExtension = [baseURL URLByAppendingPathExtension:@"qux"];
NSURL *appendComponent = [baseURL URLByAppendingPathComponent:@"qux"];

STAssertEqualObjects([appendExtension absoluteString], @"http://foo.com/bar/baz.qux", nil);
STAssertEqualObjects([appendComponent absoluteString], @"http://foo.com/bar/baz/qux", nil);

就是这样,扩展名是。(文件类型)组件是/(目录)。

所以,这两种方法都不是我想要的。我猜扩展名意味着文件扩展名,组件意味着“斜杠之间的节点”。事实上,如果您使用append with component,您添加的组件将在URL中的任何查询参数之前插入。这一点在文档中根本不清楚,这正是我提出问题的原因。我必须回到一个字符串,做一些工作,然后再转换成一个url。无论如何,这个答案是如何使用这些方法的一个很好的例子。