2011-05-10 72 views
0

當我跑我下面的代碼它顯示我像下面幫我的NSDictionary問題

警告一個錯誤:「的NSMutableDictionary」可不迴應「-setObject:forKey:forKey:」

什麼問題在我的代碼中。

myNumberOfPlayers = players; 

myDifficultyLevel = difficulty; 

myDuration = duration; 

myExcerciseId = excerciseId; 

NSMutableDictionary *myDictionary = [[ NSMutableDictionary alloc] init]; 
    [ myDictionary setObject:players forKey:players forKey:@"no_of_players"]; 
    [ myDictionary setObject:difficulty forKey:@"difficulty_level"]; 
    [ myDictionary setObject:duration forKey:@"duration_excercise"]; 
    [ myDictionary setObject:excerciseId forKey:@"Excercise_id"]; 

回答

4

它應該是

[myDictionary setObject:players forKey:@"no_of_players"]; 

代替

[myDictionary setObject:players forKey:players forKey:@"no_of_players"]; 
2

您正在調用一個不存在的方法。 你應該試試這個:

[ myDictionary setObject:players forKey:players ] 

或本

[ myDictionary setObject:players forKey:@"no_of_players"] 

我敢打賭,第二個選擇是正確的。您的代碼中可能存在拼寫錯誤,並且意外地重複了players變量。

字典可以爲確定的鍵設置對象。在同一個調用中設置一個對象給出兩個不同的鍵是沒有意義的。

2

此代碼必須像

NSMutableDictionary *myDictionary = [[ NSMutableDictionary alloc] init]; 
[myDictionary setObject:players forKey:@"no_of_players"];//you cant call for key two time. 
[myDictionary setObject:difficulty forKey:@"difficulty_level"]; 
[myDictionary setObject:duration forKey:@"duration_excercise"]; 
[myDictionary setObject:excerciseId forKey:@"Excercise_id"]; 
3

的NSMutableDictionary * myDictionary = [[ALLOC的NSMutableDictionary] INIT];

[ myDictionary setObject:players forKey:@"no_of_players"]; 
[ myDictionary setObject:difficulty forKey:@"difficulty_level"]; 
[ myDictionary setObject:duration forKey:@"duration_excercise"]; 
[ myDictionary setObject:excerciseId forKey:@"Excercise_id"];