Search code examples
iphoneobjective-ccocoansmutablearraynsmutabledictionary

Objective-C: Creating NSMutableDictionary or NSMutableArray


For my iPhone app, I want to manage values either in NSMutableDictionary or NSMutableAarray.

I want to store rating in this format

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

Here in my case number of round will be fix value suppose 10, and total player would be 2 that is also fix.

But user can submit their score in any random order but in single button click. Means

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

How can I manage dictionary or array which could help me in retrieving them again easily?


Solution

  • //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];