当我运行下面的代码时,它会向我显示如下错误
警告:'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"];
答案 0 :(得分:4)
应该是
[myDictionary setObject:players forKey:@"no_of_players"];
而不是
[myDictionary setObject:players forKey:players forKey:@"no_of_players"];
答案 1 :(得分:3)
NSMutableDictionary * myDictionary = [[NSMutableDictionary alloc] 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"];
答案 2 :(得分:2)
您正在调用不存在的方法。 你应该试试这个:
[ myDictionary setObject:players forKey:players ]
或者
[ myDictionary setObject:players forKey:@"no_of_players"]
我敢打赌,第二种选择是正确的选择。您的代码中可能存在拼写错误,并且您意外地重复了players
变量。
字典可以为确定的键设置对象。在同一个调用中设置一个给出两个不同键的对象是没有意义的。
答案 3 :(得分: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"];