将 int 转换为 NSString 时出现问题

发布于 2024-10-05 23:19:39 字数 389 浏览 3 评论 0原文

我正在开发一个使用一些 url 的 iPhone 应用程序,但在将整数附加到它们的末尾时遇到了困难。我有以下代码行

        NSURL *urlCards = [[NSURL alloc] initWithString:(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID])];

,我需要简单地在其末尾附加一个 int 。当我打印出 NSURL urlCards 的结果时,我只是获取传入的 int 值,或者 _deckID 的值。

我已经验证仅声明为本地 int 的 _deckID 在运行时确实具有正确的值。

我缺少什么?

谢谢!!

I am working on an iPhone app that is using some urls and I am running into difficulty appending ints onto the end of them. I have the following line of code

        NSURL *urlCards = [[NSURL alloc] initWithString:(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID])];

that I need to simply be appending an int to the end of. When I print out the results of the NSURL urlCards, I simply get the value of the int that I am passing in, or the value of _deckID.

I have verified that the _deckID which is declared merely as a local int does indeed have the correct value at run time.

What am I missing?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔一刀 2024-10-12 23:19:39

您遇到的是逗号运算符。逗号运算符计算每个操作数的副作用,并计算最后一个表达式的结果。例如:

int i;
int j;
int z;

z = (i = 4, j = 3, i + j);
// z is now 7

你在这里得到的:

(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID])

计算结果为 just

[NSString stringWithFormat:@"%d", _stuffID]

这是因为第一部分 @"..." 是一个没有副作用的表达式,逗号运算符的结果是[NSString stringWithFormat:] 方法的结果。

我认为你正在寻找的是这样的:

NSString *urlString = [NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID];
NSURL *urlCards = [[NSURL alloc] initWithString:urlString];

你也可以在一行中完成它,就像 KingofBliss 的答案一样。

What you've encountered is the comma operator. The comma operator evaluates each of its operands for their side effects, and evaluates to the result of the last expression. For example:

int i;
int j;
int z;

z = (i = 4, j = 3, i + j);
// z is now 7

What you've got here:

(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID])

Evaluates to just

[NSString stringWithFormat:@"%d", _stuffID]

This is because the first part @"..." is an expression that has no side effects, and the result of the comma operator is the result of the [NSString stringWithFormat:] method.

What you are looking for I think, it this:

NSString *urlString = [NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID];
NSURL *urlCards = [[NSURL alloc] initWithString:urlString];

You can also do it in one line, like KingofBliss's answer.

东走西顾 2024-10-12 23:19:39
NSString *urlString=[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID];
NSURL *urlCards = [[NSURL alloc] initWithString:urlString];

使用它可以解决你的问题。

NSString *urlString=[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID];
NSURL *urlCards = [[NSURL alloc] initWithString:urlString];

use this it solves your problem.

不疑不惑不回忆 2024-10-12 23:19:39

为什么你不能尝试这样:

NSURL *urlCards = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID]];

Why can't you try like this:

NSURL *urlCards = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文