NSMutableDictionary或NSMutableA

NSMutableDictionary或NSMutableA

对于我的iPhone应用程序,我想在NSMutableDictionaryNSMutableAarray中管理值。

我想以这种格式存储评分

round   player1  player2
  1        6        8
  2        7        9
--        --        --


在我的情况下,回合数假设为固定值10,而总玩家数为2,这也是固定值。

但是用户可以按任意随机顺序提交分数,但只需单击一次即可。手段

 score for round  "10" for both the player   enter
 score for round  "2"  for both the player   enter


如何管理字典或数组,这可以帮助我轻松地再次检索它们?

最佳答案

//You can use combination of mutable array and dictionary to handle each round and player score

NSMutableArray *mutArrayRound=[[NSMutableArray alloc] initWithCapacity:10];


//--EDIT-------------------------------------------------------------------

//You need to do it somewhere so that it'll not give error for [mutArrayRound insertObject: atIndex:]

mutDic=[[NSMutableDictionary alloc] init];

for(int i=0;i<10;i++)
{
    [mutArrayRound addObject:mutDic];
}

[mutDic release];

//-------------------------------------------------------------------------

NSMutableDictionary *mutDic=[[NSMutableDictionary alloc] initWithCapacity:2];
[mutDic setValue:@"Score_Valaue" forKey:@"player1-Score"];
[mutDic setValue:@"Score_Valaue" forKey:@"player2-Score"];

//For Round1
[mutArrayRound insertObject:mutDic atIndex:0];

//For Round2
[mutArrayRound insertObject:mutDic atIndex:1];


/*
    You can access for particular round using mutDic=[mutArrayRound objectAtIndex:0];
    And to access player score, you can use key score=[mutDic valueForKey:@"Player1_Score"];
 */

[mutArrayRound release];
[mutDic release];

10-04 21:14