xcode 将组件添加到路径 - 目标 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10896214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding components to path - Objective C
提问by Vipul Shah
I have a url say : www.gmail.com/index.php
. I also have three different strings say aa
, bb
and cc
. I want to append the three separate strings to the url in order to get a combined url in Objective C i.e I want www.gmail.com/index.php/aa/bb/cc
.
Can someone help me out ?? I am new to objective C. Thanks.
我有一个网址说:www.gmail.com/index.php
。我也有三个不同的字符串说aa
,bb
和cc
。我想将三个单独的字符串附加到 url 中,以便在 Objective C 中获得一个组合的 url,即我想要的www.gmail.com/index.php/aa/bb/cc
。有人可以帮我吗 ??我是目标 C 的新手。谢谢。
回答by gaige
If you are trying to use a URL for later use in retrieving data, etc., you're going to want an NSURL
object when you're done. You can certainly create a URL by appending @"/%@" with the next element, but you should look at doing the following:
如果您尝试使用 URL 供以后检索数据等使用,那么完成后您将需要一个NSURL
对象。您当然可以通过在下一个元素后附加 @"/%@" 来创建 URL,但您应该考虑执行以下操作:
NSURL *originalURL = [NSURL URLWithString: @"http://www.gmail.com/"];
NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: @"aa"];
NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: @"bb"];
NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: @"cc"];
This is a bit long-winded if you are adding static path elements, but if you are adding computed information it solves the problem of making sure there are appropriate slashes and allows you to use the URL construct directly.
如果您添加静态路径元素,这有点冗长,但如果您添加计算信息,它解决了确保有适当斜杠的问题,并允许您直接使用 URL 构造。
回答by Vipul Shah
Use stringByAppendingPathComponent
用 stringByAppendingPathComponent