【问题标题】:Objective-C: Creating NSMutableDictionary or NSMutableArrayObjective-C:创建 NSMutableDictionary 或 NSMutableArray
【发布时间】:2012-04-02 07:05:03
【问题描述】:

对于我的 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

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

【问题讨论】:

    标签: iphone objective-c cocoa nsmutablearray nsmutabledictionary


    【解决方案1】:

    为什么在面向对象的语言中只有数组或字典?

    @interface RoundResults : NSObject
    
    @property (nonatomic, assign) NSInteger playerOneScore;
    @property (nonatomic, assign) NSInteger playerTwoScore;
    
    @end
    
    @interface GameTotal : NSObject
    
    - (void)setResults: (RoundResults *)results forRound: (NSInteger)round;
    - (RoundResults *)resultsForRound: (NSInteger)round;
    - (NSUInteger)countOfResults;
    
    @end
    

    【讨论】:

    • 我们在 Objective-C 中有类和对象,因此您不仅限于内置的集合类型。如果您像我展示的那样定义类,那么与使用字典和数组相比,您可以在数据结构中传达更多含义。反过来,您可以使代码更易于理解和更改;并且您隐藏了如何存储这些结果的详细信息,以便以后可以使用不同的算法或存储。
    【解决方案2】:
    //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];
    

    【讨论】:

    • 嘿,如果我想添加类似 [mutArrayRound insertObject:mutDic atIndex:1];首先和 [mutArrayRound insertObject:mutDic atIndex:0];意思是不按顺序那么怎么管理
    • 对于不同的轮次,您可以将第 1 轮作为数组的索引,例如 [mutArrayRound insertObject:mutDic atIndex:round-1];,因为您已指定轮次介于 1 到 10 之间。
    • 感谢重播,但我的问题是,如果从 0 到 8 的索引上没有任何对象,是否允许我先在 objectatIndex:9 插入
    • 好的,谢谢,我试过了,但不知怎的,我会再试一次。
    • @ios,我也查了一下,显示超出索引的错误。好吧,算了。我要问你。在你的游戏中,具体的流程是什么?我猜如果用户完成第一轮,那么只去第二轮?并且喜欢所有剩余回合的虎钳?还是直接打6、8、7等?
    【解决方案3】:

    你可以制作NSDictionary的NSMutableArray:

          NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
                    // Now say Player one got 50 and Player2 got 65 in first round then add them in dictionary
    
    
                    NSDictionary *dict  = [[NSDictionary alloc] initWithObjectsAndKeys:@"50",@"P1",@"65",@"P2", nil];
                    [arr addObject:dict];
    [dict release];
    
    
                // As soon as you got the sores make a NSDictionary object and init it with objects like above and add that to the mutable array . It will add it to next index.
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的建议,在我的情况下,问题是:如果用户先添加第 9 轮的分数,然后添加第 1 轮的分数,那么如何管理
    • 我认为..首先,您可以为 P1 制作 10 个 dicts,P1 得分为“0”,P2 得分为“0”。将所有 10 个字典添加到可变数组后,您可以在特定索引处替换 NSDictionary 对象,无论用户输入的整数是什么。
    【解决方案4】:

    想法 1:

    NSMutableDictionary* playerArounds = [NSMutableDictionary dictionary];
    NSMutableDictionary* playerBrounds = [NSMutableDictionary dictionary];
    
    [playerArounds setObject:@"5" forKey:@"1"]; 
    // Player A scored 5 for round 1
    
    // etc...
    

    想法 2:(适合 >2 名玩家)

    NSMutableArray* playerRounds = [NSMutableArray arrayWithObjects:nil];
    
    for (int i=0; i<10; i++)
        [playerRounds addObject:[NSMutableDictionary dictionary]];
    
    [[playerRounds objectAtIndex:0] setObject:@"5" forKey:@"1"];  
    // Player 0 scored 5 for round 1
    
    // etc...
    

    想法 3:(更纯 C 的方法)

    // a 2-dimensional array
    int scores[2][10]; 
    
    scores[0][2] = 5;
    
    // Player 0's score for round 3 (2+1) is 5
    

    正如所指出的:

    而不是例如[playerArounds setObject:@"5" forKey:@"1"];,因为您的值将是 int(而不是字符串),所以您最好使用(它会更有意义):

    例如[playerArounds setObject:[NSNumber numberWithInt:5] forKey:[NSNumber numberWithInt:1]];

    【讨论】:

    • 使用setObject:forKey: 将对象放入字典中。
    • 并使用 NSNumbers 作为键。
    • @JeremyP setObject:forKey: 好点(刚刚更正了)。现在至于使用例如[NSNumber numberWithInt:5] 而不是 @"5" 显然是 OP 如果按照建议实现它的方式。我只是避免这样做,以使代码更具...可读性。不过,这也很好......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多