为什么这段代码有错误?

时间:2012-01-30 18:21:33

标签: objective-c

我有:

boardValue = [NSNumber numberWithInteger: 2];
NSDictionary * dict = [NSDictionary dictionaryWithValuesForKeys: @"sample", @"word", boardValue , @"value", nil];

这与以下示例非常相似:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil];

来自Apple的文件: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsdictionary_Class/Reference/Reference.html

我得到错误“方法调用的参数太多,期望1,有5”。有什么问题?

2 个答案:

答案 0 :(得分:2)

dictionaryWithValuesForKeys:an array作为参数,而不是变量参数列表。

(另外,我相信它是一个实例方法,而不是类方法,所以[NSDictionary dictionaryWithValuesForKeys:args]`将不起作用。)

答案 1 :(得分:1)

请注意您的代码与文档进行比较。你想打电话......

boardValue = [NSNumber numberWithInteger: 2]; 
NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys: @"sample", @"word", boardValue , @"value", nil];
相关问题