2012-04-02 118 views
0

對於我的iPhone應用程序,我想要管理NSMutableDictionaryNSMutableAarray中的值。Objective-C:創建NSMutableDictionary或NSMutableArray

我想存儲的評價此格式

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

在這裏圓我的病例數將是固定值假設10,總播放器將是2,它也修復。

,但用戶可以在任何隨機順序,但在單一的按鈕click.Means

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

提交他們的成績因此,如何管理字典或數組,可以幫助我再次輕鬆地檢索它們?

請幫助和建議。

由於

回答

3
//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]; 
+0

哎,如果我想添加像[mutArrayRound insertObject:mutDic atIndex:1];第一個和[mutArrayRound insertObject:mutDic atIndex:0];表示不按順序然後如何管理 – ios 2012-04-02 10:39:41

+0

對於不同的輪可以傳遞輪-1作爲指標像陣列'[mutArrayRound insertObject:mutDic atIndex:圓形-1];'如你指定的輪之間是1〜10。 – Hemang 2012-04-02 12:23:34

+0

感謝重播,但我的問題是,它是否會允許我在objectatIndex插入:9第一,如果有索引上沒有任何物體從0到8個 – ios 2012-04-02 12:38:59

1

理念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]];

+0

使用'setObject:forKey:'將對象放入字典中。 – JeremyP 2012-04-02 07:19:08

+1

並使用NSNumbers作爲密鑰。 – JeremyP 2012-04-02 07:20:16

+0

@JeremyP'setObject:forKey:'的好處(只是改正了它)。至於使用例如'[NSNumber numberWithInt:5]'而不是'@「5」'顯然是OP如果按照建議實現它的方式。我只是避免這樣做,以使代碼更具可讀性。好點的也一樣,雖然... – 2012-04-02 07:24:24

2

您可以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. 

希望這會有所幫助。

+0

感謝的建議,在這裏我的情況的問題是:如果用戶先加比分爲圓形,然後第9輪和1後記如何管理 – ios 2012-04-02 10:37:20

+1

我覺得..這個你可以10種類型的字典用「0」 P1也得分,P2也是「0」。將所有10個http://stardict.sourceforge.net/Dictionaries.php下載到可變數組後,可以更換在特定索引的NSDictionary對象不管用戶對於輪數進入。 – 2012-04-02 10:48:26

+1

@NeelamVerma:+1了一個很好的答案:) – 2012-04-09 07:54:51

4

爲什麼只有數組或字典是面向對象的語言?

@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 
+0

感謝您的幫助你能更詳細地解釋 – ios 2012-04-02 10:46:22

+0

我們有類和對象的Objective-C,這樣你就不會僅限於內置的集合類型。如果你定義了類似我所展示的那些類,那麼你可以在數據結構中傳遞比使用字典和數組更多的意義。反過來,你使代碼更容易理解和改變;並且隱藏瞭如何存儲這些結果的細節,以便您稍後可以使用不同的算法或存儲。 – 2012-04-02 13:06:28

+0

感謝您的建議。 – ios 2012-04-12 06:02:32

相關問題