本文介绍了方法调用的参数太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的 appDelegate 中的 NSString 设置 Twitter 消息在我的应用程序中应该说的内容的初始文本.在此处查看代码:

 NSString *tweet;tweet=[MyWebFunction tweet:appDelegate.stadium_id];if([deviceType hasPrefix:@"5."]){//创建视图控制器TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];[twitter setInitialText:@"@%",tweet];

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

非常感谢任何帮助.:)

解决方案

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

在您的情况下,您只需调用:

[twitter setInitialText:tweet];

或致电:

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

编辑
我觉得有必要补充一点,为了进一步理解,当一个方法的声明以 ...stringWithFormat
)/p>

例如,查看 NSString 的文档显示 stringWithFormat 是这样声明的:

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

类似地,NSArray 中的 arrayWithObjects 声明如下:

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

哪个会使用:

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

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];

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

解决方案

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];

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

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

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

Similarly, arrayWithObjects in NSArray is declared as such:

+(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];

这篇关于方法调用的参数太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:27