-1

Im new to objective-c. I'm trying to make a calculator application. When I run this code:

-(void) addNums: (int)fnum: (int)snum {
    ans = fnum + snum;
    [result setStringValue:@"%i",ans];
}

to try and set the result label I get the error "Too many arguments to method call, expected 1, have 2". I was expecting this to work the same way as NSLog formatting.

Thanks for the help. Let me know if you need to see more of my code.

4

1 回答 1

1

您需要做的就是使用该NSString方法+ stringWithFormat:,所以您需要做的就是

[result setStringValue:[NSString stringWithFormat:@"%d", ans]];

为了更好地理解NSString和方法,+ stringWithFormat:请参阅NSString 上的 Apple 文档。

我还注意到你的整个方法没有意义。请尝试以下

- (void)addNum:(int)fnum withNum:(int)snum {
    int ans = fnum + snum;
    [result setText:[NSString stringWithFormat:@"%d", ans]];
}

缺少什么

1)你有-(void) addNums: (int)fnum: (int)snum它应该是- (void)addNum:(int)fnum withNum:(int)snum。查看Learning Objective-c A Primar并查看标题为Methods and Messages这将向您展示如何正确声明方法的部分。

2)你没有声明ans,而不是ans = fnum + snum应该是int ans = fnum + snum;

3)最后你称之为resultaUILabel因为这setStringValue:不是一个真正的方法,因为UILabel它应该是setText:所以这条线应该是[result setText:[NSString stringWithFormat:@"%d", ans]];或者你可以在 UILabel 上result.text = [NSString stringWithFormat:@"%d", ans];查看Apple 文档。

于 2013-09-02T20:02:30.017 回答