当我尝试在iOS应用程序中传递NSURL中的参数时,为什么会出现此错误? [英] Why do I get this error when I try to pass a parameter in NSURL in iOS app?

查看:109
本文介绍了当我尝试在iOS应用程序中传递NSURL中的参数时,为什么会出现此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我在公共方法中所拥有的 - (IBAction)methodName

This is what I have in a public method - (IBAction)methodName

NSString *quoteNumber = [[self textBox] text];

NSURL *url = [[NSURL alloc] initWithString:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];

我得到的错误是:


方法调用的参数太多,预期为1,有2

Too many arguments to method call, expected 1, have 2

我做错了什么?

推荐答案

initWithString 方法只接受普通的NSString,你传递它格式化的NSString,看看这段代码:

The initWithString method can only accept a normal NSString, you are passing it a formatted NSString, Take a look at this code:

    NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]];

这可能有点令人困惑,你可以按如下方式分解:

That might be a bit confusing, you can break it up as follows:

NSString *urlString = [NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber];

NSURL *url = [[NSURL alloc] initWithString:urlString];

现在您的字符串格式正确, NSURL initWithString 方法将起作用!

Now your string is properly formated, and the NSURL initWithString method will work!

此外,为了将来你更清楚,你可以在设置时利用Objective-C的点符号语法 quoteNumber 字符串,如下所示:

Also, just so it is clearer for you in the future, you can take advantage of Objective-C's dot notation syntax when you set your quoteNumber string, as follows:

NSString *quoteNumber = self.textBox.text;

此外,您试图将此引号传递到您的 urlString 作为一个数字(如%d 所示),请记住 quotedNumber 是一个NSString对象,当您尝试将其传递给 stringWithFormat 方法时会崩溃。您必须先将字符串转换为NSInteger或NSUInteger。

Also, you are trying to pass this quoted number into your urlString as a digit (as seen with the %d), remember that quotedNumber is a NSString object, and will crash when you try to pass it to the stringWithFormat method. You must convert the string first to a NSInteger, or NSUInteger.

请参阅这个问题如何做到这一点(不要担心它很容易)!

Please refer to this SO question to how to do that (don't worry it's very easy)!

这篇关于当我尝试在iOS应用程序中传递NSURL中的参数时,为什么会出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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