将 int 转换为 NSString 时出现问题
我正在开发一个使用一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您遇到的是逗号运算符。逗号运算符计算每个操作数的副作用,并计算最后一个表达式的结果。例如:
你在这里得到的:
计算结果为 just
这是因为第一部分
@"..."
是一个没有副作用的表达式,逗号运算符的结果是[NSString stringWithFormat:]
方法的结果。我认为你正在寻找的是这样的:
你也可以在一行中完成它,就像 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:
What you've got here:
Evaluates to just
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:
You can also do it in one line, like KingofBliss's answer.
使用它可以解决你的问题。
use this it solves your problem.
为什么你不能尝试这样:
Why can't you try like this: