Iphone 目标C:创建NSMutableDictionary或NSMutableArray

Iphone 目标C:创建NSMutableDictionary或NSMutableArray,iphone,objective-c,cocoa,nsmutablearray,nsmutabledictionary,Iphone,Objective C,Cocoa,Nsmutablearray,Nsmutabledictionary,对于我的iPhone应用程序,我想在NSMutableDictionary或NSMutableAarray中管理值 我想以这种格式存储评级 round player1 player2 1 6 8 2 7 9 -- -- -- 在我的例子中,假设10,回合数是固定值,总玩家数是2,这也是固定值 但用户可以提交他们的分数在任何随机顺序,但在一个按钮点击。意味着 score for round "1

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

我想以这种格式存储评级

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

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

想法1:

NSMutableDictionary* playerArounds = [NSMutableDictionary dictionary];
NSMutableDictionary* playerBrounds = [NSMutableDictionary dictionary];

[playerArounds setObject:@"5" forKey:@"1"]; 
// Player A scored 5 for round 1

// etc...
创意2:(如果超过2名玩家,则适用)


如前所述:

NSMutableDictionary* playerArounds = [NSMutableDictionary dictionary];
NSMutableDictionary* playerBrounds = [NSMutableDictionary dictionary];

[playerArounds setObject:@"5" forKey:@"1"]; 
// Player A scored 5 for round 1

// etc...
而不是例如
[playerRounds setObject:@“5”forKey:@“1”],因为您的值将是int(而不是字符串),所以最好使用(这会更有意义):

e、 g.
[PlayerRounds setObject:[NSNumber numberWithInt:5]forKey:[NSNumber numberWithInt:1]]

//您可以使用可变数组和字典的组合来处理每一轮和玩家得分
//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];
NSMUTABLEARRY*mutArrayRound=[[NSMUTABLEARRY alloc]initWithCapacity:10]; //--编辑------------------------------------------------------------------- //您需要在某个地方执行此操作,以便它不会为[mutArrayRound insertObject:atIndex:]提供错误 mutDic=[[NSMutableDictionary alloc]init];
对于(int i=0;i,您可以创建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.

希望这会有所帮助。

为什么在面向对象语言中只使用数组或字典

@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

使用
setObject:forKey:
将对象放入字典。并使用NSNumber作为键。@JeremyP
setObject:forKey:
(刚刚更正了它)。现在使用例如
[NSNumber numberwhithint:5]
代替
@“5”
显然是OP按照建议实施的方式。我只是避免这样做,以使代码更具可读性。不过,这也很好……感谢您的建议,在我的案例中,问题是:如果用户先添加第9轮和第1轮的分数,那么如何管理我认为……首先,您可以10个dict,P1的分数为“0”,P2的分数也为“0”。将所有10个dict添加到可变数组中后,您可以在特定索引处替换NSDictionary对象,无论用户为整数输入什么。嘿,如果我想添加类似[mutArrayRound insertObject:mutDic atIndex:1];首先和[mutArrayRound insertObject:mutDic atIndex:0];表示不按顺序,那么如何管理不同的轮您可以将轮1作为数组的索引传递,如
[mutArrayRound insertObject:mutDic atidex:round-1]
正如您所指定的,您的轮次在1到10之间。谢谢您的重播,但我的问题是,如果在0到8之间的索引中没有任何对象,是否允许我先插入objectatIndex:9。好的,谢谢,我尝试了,但不知怎的,它对我不起作用,我将重试。@ios,我也检查了它,它显示了超出索引的错误。好的,让它为f我想问你。在你的游戏中,确切的流程是什么?我猜如果用户完成第一轮,然后只进行第二轮?并且像vise一样完成所有剩余的轮?或者直接玩第6轮、第8轮或第7轮等等?我们在Objective-C中有类和对象,所以你不局限于内置的集合类型。如果你定义像与使用字典和数组相比,我向您展示的这些方法可以在数据结构中传达更多的含义。反过来,您可以使代码更易于理解和更改;您还可以隐藏存储这些结果的详细信息,以便以后可以使用不同的算法或存储。
@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