[英]How do I add a number to an NSURL? Too many arguments error
I've got a small problem that seems a little bit odd to me. 我有一个小问题,对我来说似乎有点奇怪。 I often used NSString or NSLog while adding NSNumbers into several places: 在将NSNumbers添加到几个地方时,我经常使用NSString或NSLog:
NSNumber *categoryId = [[NSNumber alloc]initWithInt:0];
NSURL *url = [NSURL URLWithString:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
Now xcode tells me that I'm too many arguments. 现在,xcode告诉我,我的参数太多了。 What am I doing wrong? 我究竟做错了什么? Setting up an NSNumber into NSStrings or NSLogs works as I did it above. 将NSNumbers设置为NSStrings或NSLogs的工作原理与上面的操作相同。
Best Regards 最好的祝福
What is wrong is on 怎么了
NSURL *url = [NSURL URLWithString:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
you are calling URLWithString:
and then pass in a string that is not being formatted correctly. 您正在调用URLWithString:
,然后传入未正确格式化的字符串。 If you want to do it all on one line then you need to be using stringWithFormat:
like 如果要在一行上完成所有操作,则需要使用stringWithFormat:
like
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]]];
Because it is adding a parameter you can't just create a string like you normally would with @"some text"
you need to format it using the stringWithFormat:
which will return an NSString *
with the text held within @""
and the paramters you pass in. So [NSString stringWithFormat:@"My String will come with %@", @"Apples"];
因为它添加了一个参数,所以您不能像通常使用@"some text"
那样创建一个字符串,您需要使用stringWithFormat:
对其进行格式化,这将返回一个NSString *
,其中文本包含在@""
和参数中您将传入。因此[NSString stringWithFormat:@"My String will come with %@", @"Apples"];
this would provide an NSString
with "My String will come with Apples"
. 这将为NSString
提供"My String will come with Apples"
。 For more information check out the Apple Documentation for NSString
and stringWithFormat:
有关更多信息,请查看Apple文档中的NSString
和stringWithFormat:
Try this : 尝试这个 :
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://shop.rs/api/json.phpaction=getCategoryByCategory&category=%i", [categoryId integerValue]]];
Initially code was wrong because of : "categoryId integerValue]" (I forgot a '['). 最初的代码是错误的,因为:“ categoryId integerValue]”(我忘记了'[')。
You can use NSString
to form your NSURL
. 您可以使用NSString
形成您的NSURL
。 You can then pass it to your URLWithString
like below: 然后可以将其传递给您的URLWithString
如下所示:
NSNumber *categoryId = [NSNumber numberWithInteger:0];
NSString *urlString = [NSString stringWithFormat:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
NSURL *url = [NSURL URLWithString:urlString];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.