2012-05-08 74 views
-1

我已經解決了包含終止方法的問題。NSMutableArray shuffle issue

If(indexes==0){ 
endUp = YES; 
} 

感謝您指點正確的方向。這不是洗牌的問題。 Alessign

+0

使用更新刪除原始問題不會幫助其他人閱讀您的問題,遇到同樣的問題。相反,讓問題是,並接受正確的答案,以幫助其他人知道_both_問題和答案 –

+0

你做了什麼MR。 Alessign?這篇文章是否幫助任何一個? –

回答

2

該錯誤可能在您用來洗牌第一個數組的循環中。 你不會發布你的代碼的一部分...是這樣的嗎?

for (int i=0; i<[indexes count]; i++){ 
// (...) 
     [indexes removeObjectAtIndex:index]; 
// (...) 
} 

這可能會更好:

int arrayCount = [indexes count]; 
for (int i=0; i<arrayCount; i++){ 
// (...) 
     [indexes removeObjectAtIndex:index]; 
// (...) 
} 

這個完整的代碼工作得很好,沒有任何錯誤或崩潰:

int length = 10; 
NSMutableArray* indexes = [[NSMutableArray alloc] initWithCapacity:length]; 

for (int i=0; i<10; i++) [indexes addObject:[NSNumber numberWithInt:i]]; 
NSMutableArray*shuffle = [[NSMutableArray alloc] initWithCapacity:length]; 


int arrayCount = [indexes count]; 
for (int i=0; i<arrayCount; i++){ 

    int index = arc4random()%[indexes count]; 
    NSLog(@"___index: %i", index); 
    NSLog(@"indexes: %@ ", indexes); 

    [shuffle addObject:[indexes objectAtIndex:index]]; 
    [indexes removeObjectAtIndex:index]; 
    NSLog(@"shuffle: %@ ", shuffle); 
} 


for (int i=0; i<[shuffle count]; i++){ 
    int questionNumber = [[shuffle objectAtIndex:i] intValue] + 1; 
    NSLog(@"questionNumber: %i ", questionNumber); 

} 
+0

謝謝您的建議!上面的代碼的問題是,如果方法在大括號中,它將一起生成所有輸出,並且當第二個循環到來時,它將僅重複最後一個並且也會乘以數組,因爲循環從頂部開始的代碼底部,所以它創建無限數組。 – Alessign

+0

我試圖執行'if'語句,但根本沒有任何影響if(questionNumber == 10 && [shuffle count] <10){restartGame = NO; }' – Alessign

+0

當然,這只是一個示例,我不知道你的遊戲邏輯在你的問題之外......你應該根據你的需求實現代碼 – meronix

-1

我明白了,反正,知道了!我實現了一個'if'語句,它將終止它! if ([indexes count] == 0)
{endProcess = YES}

+0

你做了什麼,它沒有用。 –