方法调用的参数过多 [英] Too many arguments to method call
问题描述
我正在尝试使用来自appDelegate的NSString设置Twitter消息应在我的应用中说的内容的初始文本。在此处查看代码:
I am trying to set the initial text for what the twitter message should say in my app using a NSString from my appDelegate. Check out the code here:
NSString *tweet;
tweet=[MyWebFunction tweet:appDelegate.stadium_id];
if([deviceType hasPrefix:@"5."]){
// Create the view controller
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"@%",tweet];
问题是,twitter setInitialText上是否存在错误,即方法有太多参数呼叫,预期1,有2。?!?!?
The problem is, is there is an error at the twitter setInitialText that there are Too many arguments to method call, expected 1, have 2. ?!?!?
任何帮助将不胜感激。 :)
Any help is greatly appreciated. :)
推荐答案
仅 TWTweetComposeViewController
方法 setInitialText
接受一个参数,类型为 NSString *
。您不能使用 NSString
方法<$ c $来简单格式化传递给方法的任何和所有 NSString
变量。 c> stringWithFormat (我想这是您看到的语法 [NSString stringWithFormat:@%@,myString]
) 。
The TWTweetComposeViewController
method setInitialText
only takes one argument, being of type NSString*
. You cannot simply format any and all NSString
variables passed to a method as you can with the NSString
method stringWithFormat
(which is, I imagine, where you've seen the syntax [NSString stringWithFormat:@"%@", myString]
).
在您的情况下,您只需简单地致电:
In your case, you either need to simply call:
[twitter setInitialText:tweet];
或致电:
[twitter setInitialText:[NSString stringWithFormat:@"%@", tweet]]
编辑
为了使您进一步理解,我认为有必要添加一个方法仅接受可变数量的参数(例如 stringWithFormat
),当其声明以 ...
结尾时,例如 NSString
的文档显示, stringWithFormat
的声明如下:
For example, looking in the docs for NSString
reveals that stringWithFormat
is declared as such:
+(id) stringWithFormat:(NSString *)format, ...;
类似地,<$ c $中的 arrayWithObjects
c> NSArray 这样声明:
Similarly, arrayWithObjects
in NSArray
is declared as such:
+(id) arrayWithObjects:(id)firstObj, ...;
一个人会这样使用:
NSString* myString1 = @"foo";
NSString* myString2 = @"bar";
NSNumber* myNumber = [NSNumber numberWithInt:42];
NSArray* myArray = [NSArray arrayWithObjects:myString1, myString2, myNumber, nil];
这篇关于方法调用的参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!