我有一个小问题,我觉得有点奇怪。我经常在将 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]];
现在 xcode 告诉我参数太多了。我究竟做错了什么?将 NSNumber 设置为 NSStrings 或 NSLogs 的工作方式与我在上面所做的一样。
最好的问候
最佳答案
哪里出了问题
NSURL *url = [NSURL URLWithString:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
您正在调用 URLWithString:
,然后传入一个格式不正确的字符串。如果您想在一行上完成所有操作,那么您需要使用 stringWithFormat:
之类的
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]]];
因为它添加了一个参数,所以您不能像通常使用 @"some text"
那样创建一个字符串,您需要使用 stringWithFormat:
对其进行格式化这将返回一个 NSString *
,其中包含 @""
中的文本和您传入的参数。因此 [NSString stringWithFormat:@"My String 将随附%@", @"Apples"];
这将提供一个带有 "My String will come with Apples"
的 NSString
。有关更多信息,请查看 Apple Documentation for NSString
and stringWithFormat:
关于ios - 如何将数字添加到 NSURL?参数错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367268/