我正在尝试对 NSMutableArray 进行洗牌,以便每次有人加载视图时它的顺序都会混淆。
在我的-(void)viewDidLoad
我把下面的代码(如其他用户的建议):
NSMutableArray *shuffleTwo = [self.chosenTeamDict objectForKey:@"clubs"];
int random = arc4random() % [shuffleTwo count];
for (int i = 0; i < [shuffleTwo count]; i++) {
[shuffleTwo exchangeObjectAtIndex:random withObjectAtIndex:i];
}
NSLog(@"%@", shuffleTwo);
但是当我这样做并尝试运行该页面时,我收到以下错误:
2012-07-09 18:42:16.126 Kit-Quiz[6505:907] (null)
libc++abi.dylib: terminate called throwing an exception
谁能建议一种改组这个数组的新方法,或者建议我如何避免这个错误..!?我正在为 iOS 5 构建,我正在使用 Xcode45-DP1。提前致谢!
(编辑)
我也试过这个方法,我得到了同样的错误:
NSMutableArray *shuffledArray = [[NSMutableArray alloc] init];
NSMutableArray *standardArray = [self.chosenTeamDict objectForKey:@"clubs"];
for(int s = 0; s < [standardArray count]; s++){
int random = arc4random() % s;
[shuffledArray addObject:[standardArray objectAtIndex:random]];
}
NSLog(@"%@", shuffledArray);