我将您的代码粘贴到我正在开发的应用程序中。
例程被调用 x 次:
for (int i = 0; i < 5; i++) {
[self trySort];
}
例程加载一个数组,对其进行排序,然后打印它:
- (void)trySort {
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", nil];
//code pasted from question
int count = [arr count];
for (int i = 0; i < count; i++) {
int n = arc4random_uniform(count - i) + i;
[arr exchangeObjectAtIndex:i withObjectAtIndex:n];
}
//
NSLog(@"%@%@%@%@%@%@%@%@%@%@%@%@", [arr objectAtIndex:0], [arr objectAtIndex:1], [arr objectAtIndex:2], [arr objectAtIndex:3], [arr objectAtIndex:4], [arr objectAtIndex:5], [arr objectAtIndex:6], [arr objectAtIndex:7], [arr objectAtIndex:8], [arr objectAtIndex:9], [arr objectAtIndex:10], [arr objectAtIndex:11]);
}
结果被合理排序:
2013-04-07 16:24:50.923 xxx[2122:c07] BIAKHFDEGCLJ
2013-04-07 16:24:50.927 xxx[2122:c07] DGICJHLBAKFE
2013-04-07 16:24:50.928 xxx[2122:c07] HIGEFADJLCKB
2013-04-07 16:24:50.928 xxx[2122:c07] IHFDBJEALCKG
2013-04-07 16:24:50.928 xxx[2122:c07] GBEHIFCKAJDL
是否有可能在您的数组加载和排序之间发生阻止排序的事情?