方法调用的参数太多

时间:2021-04-10 23:21:30

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:

我正在尝试使用appDelegate中的NSString设置twitter消息应该在我的应用中说的内容的初始文本。看看这里的代码:

    NSString *tweet;
tweet=[MyWebFunction tweet:appDelegate.stadium_id];


if([deviceType hasPrefix:@"5."]){

    // Create the view controller
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

    [twitter setInitialText:@"@%",tweet];

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. ?!?!?

问题是,在twitter setInitialText中是否存在错误,即方法调用的参数太多,预期为1,有2.?!?!?

Any help is greatly appreciated. :)

任何帮助是极大的赞赏。 :)

方法调用的参数太多

3 个解决方案

#1


6  

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]).

TWTweetComposeViewController方法setInitialText只接受一个参数,类型为NSString *。您不能简单地格式化传递给方法的任何和所有NSString变量,就像使用NSString方法stringWithFormat一样(我想,在那里您已经看到了语法[NSString stringWithFormat:@“%@”,myString])。

In your case, you either need to simply call:

在您的情况下,您需要简单地调用:

[twitter setInitialText:tweet];

or call:

[twitter setInitialText:[NSString stringWithFormat:@"%@", tweet]]

EDIT
I feel it necessary to add, to further your understanding, that a method only takes a variable number of arguments (such as stringWithFormat) when its declaration ends with ...

编辑我认为有必要添加,以进一步理解,一个方法只有在其声明结束时才采用可变数量的参数(如stringWithFormat)...

For example, looking in the docs for NSString reveals that stringWithFormat is declared as such:

例如,查看NSString的文档会发现stringWithFormat是这样声明的:

+(id) stringWithFormat:(NSString *)format, ...;

Similarly, arrayWithObjects in NSArray is declared as such:

类似地,NSArray中的arrayWithObjects声明为:

+(id) arrayWithObjects:(id)firstObj, ...;

which one would use like:

哪一个会用到:

NSString* myString1 = @"foo";
NSString* myString2 = @"bar";
NSNumber* myNumber = [NSNumber numberWithInt:42];
NSArray* myArray = [NSArray arrayWithObjects:myString1, myString2, myNumber, nil];

#2


0  

Try [twitter setInitialText:tweet];

试试[twitter setInitialText:tweet];

If you really need formatted text for a more complex case, try

如果您确实需要格式化文本来处理更复杂的案例,请尝试

[twitter setInitialText:[NSString stringWithFormat:@"%@", tweet]];

[twitter setInitialText:[NSString stringWithFormat:@“%@”,tweet]];

#3


0  

"[twitter setInitialText:@"@%",tweet];"

you just got your "@" and your "%" the wrong way round it should be

你只是得到了你的“@”和你的“%”错误的方式应该是

[twitter setInitialText:@"**%@**",tweet];

#1


6  

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]).

TWTweetComposeViewController方法setInitialText只接受一个参数,类型为NSString *。您不能简单地格式化传递给方法的任何和所有NSString变量,就像使用NSString方法stringWithFormat一样(我想,在那里您已经看到了语法[NSString stringWithFormat:@“%@”,myString])。

In your case, you either need to simply call:

在您的情况下,您需要简单地调用:

[twitter setInitialText:tweet];

or call:

[twitter setInitialText:[NSString stringWithFormat:@"%@", tweet]]

EDIT
I feel it necessary to add, to further your understanding, that a method only takes a variable number of arguments (such as stringWithFormat) when its declaration ends with ...

编辑我认为有必要添加,以进一步理解,一个方法只有在其声明结束时才采用可变数量的参数(如stringWithFormat)...

For example, looking in the docs for NSString reveals that stringWithFormat is declared as such:

例如,查看NSString的文档会发现stringWithFormat是这样声明的:

+(id) stringWithFormat:(NSString *)format, ...;

Similarly, arrayWithObjects in NSArray is declared as such:

类似地,NSArray中的arrayWithObjects声明为:

+(id) arrayWithObjects:(id)firstObj, ...;

which one would use like:

哪一个会用到:

NSString* myString1 = @"foo";
NSString* myString2 = @"bar";
NSNumber* myNumber = [NSNumber numberWithInt:42];
NSArray* myArray = [NSArray arrayWithObjects:myString1, myString2, myNumber, nil];

#2


0  

Try [twitter setInitialText:tweet];

试试[twitter setInitialText:tweet];

If you really need formatted text for a more complex case, try

如果您确实需要格式化文本来处理更复杂的案例,请尝试

[twitter setInitialText:[NSString stringWithFormat:@"%@", tweet]];

[twitter setInitialText:[NSString stringWithFormat:@“%@”,tweet]];

#3


0  

"[twitter setInitialText:@"@%",tweet];"

you just got your "@" and your "%" the wrong way round it should be

你只是得到了你的“@”和你的“%”错误的方式应该是

[twitter setInitialText:@"**%@**",tweet];