2012-01-30 127 views
-1

我:爲什麼這個代碼有錯誤?

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

這非常類似於下面的例子:

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

從蘋果單證在: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsdictionary_Class/Reference/Reference.html

我得到的錯誤「太多的參數的方法調用,期望1,有5「。問題是什麼?

回答

2

dictionaryWithValuesForKeys:an array作爲參數,而不是變量的參數列表。

(另外,我認爲它是一個實例方法,而不是一個類的方法,所以[NSDictionary的dictionaryWithValuesForKeys:參數]`將無法工作。)

1

通知你的代碼相比文檔。您想致電...

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