[英]Objective-C: Creating NSMutableDictionary or NSMutableArray
對於我的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
如何管理字典或數組,這可以幫助我輕松地再次檢索它們?
為什么只使用面向對象的語言中的數組或字典?
@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
//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];
您可以使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.
希望這會有所幫助。
理念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]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.