向路径添加组件 - 目标C. [英] Adding components to path - Objective C

查看:337
本文介绍了向路径添加组件 - 目标C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址说: www.gmail.com/index.php 。我还有三个不同的字符串,例如 aa bb cc 。我想将三个单独的字符串附加到url以获得Objective C中的组合url,即我想要 www.gmail.com/index.php/aa/bb/cc
有人可以帮帮我吗?我是目标C的新手。谢谢。

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.

推荐答案

如果您尝试使用URL以便以后用于检索数据等。完成后,你将需要一个 NSURL 对象。您当然可以通过在下一个元素后附加@/%@来创建一个URL,但是您应该看看如下:

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:

NSURL *originalURL = [NSURL URLWithString: @"http://www.gmail.com/"];
NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: @"aa"];
NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: @"bb"];
NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: @"cc"];

如果要添加静态路径元素,这有点啰嗦,但是如果要添加计算机信息它解决了确保有适当的斜杠的问题,并允许您直接使用URL构造。

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.

这篇关于向路径添加组件 - 目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆