2012-12-21 29 views
2

林。抛去阵列捕获的异常

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray exchangeObjectAtIndex:withObjectAtIndex:]: index 16 beyond bounds [0 .. 3]' 

我洗牌标签值的阵列码,

words = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil] ; 

NSUInteger count = [questar count]; 
for (NSUInteger i = 0; i < count; ++i) { 
    // Select a random element between i and end of array to swap with. 
    NSInteger nElements = count - i; 
    NSInteger n = (arc4random() % nElements) + i; 
    [words exchangeObjectAtIndex:i withObjectAtIndex:n]; 
} 

,我应该做出什么样的变化?任何一个可以帮助我解决

+1

在哪里'count'变量定义了吗? – Eimantas

+0

@Eimantas对不起。现在问题更新 –

+1

什么是questar? – Ilanchezhian

回答

0
[words exchangeObjectAtIndex:i withObjectAtIndex:n]; 

在上面的语句,既in应该有一个值比words数少(这里是4)。否则,你会得到例外。

1

你必须首先从quester选择图像。你创建索引nquester数组中取对象没有?

words = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil] ; 

NSUInteger count = [questar count]; 
for (NSUInteger i = 0; i < count; ++i) 
{ 
    // Select a random element between i and end of array to swap with. 
    NSInteger nElements = count - i; 
    NSInteger n = (arc4random() % nElements) + i; 

    currentImage = [questar objectAtIndex:n]; 

    [words replaceObjectAtIndex:i withObject:currentImage]; 
} 

或者,如果你想为阵列变化count内交换:

NSUInteger count = [words count]; 
0

以下:

NSInteger nElements = count - i; 
NSInteger n = (arc4random() % nElements) + i; 

应改为:

NSInteger nElements = count; 
NSInteger n = (arc4random() % nElements);