我有一个名为mainArray的数组。假设它包含Red, Orange, Yellow, Green, Blue。
我希望使用其他两个数组( shuffleArrayIndex和shuffleArrayRand )中的值重新排序该shuffleArrayIndex。
[mainArray exchangeObjectAtIndex:x withObjectAtIndex:randInt];
索引值x将来自shuffleArrayIndex。假设shuffleArrayIndex包含1, 3, 4, 2, 0。
withObjectAtIndex randInt将来自shuffleArrayRand。假设shuffleArrayRand包含2, 1, 3, 4, 1。
因此,我想创建一个循环,它将交换mainArray的对象5次(mainArray的.count)。
因此,第一个交易所看起来是:
[mainArray exchangeObjectAtIndex:1 withObjectAtIndex:2];
第二项是:
[mainArray exchangeObjectAtIndex:3 withObjectAtIndex:1];
第三项:
[mainArray exchangeObjectAtIndex:4 withObjectAtIndex:3];
那么,我如何创建一个循环来执行此操作,该循环将与mainArray.count进行多次交换。
因此,如果mainArray有50个对象,而shuffleArrayIndex和shuffleArrayRand有50个对象,那么循环将交换mainArray的对象50次。
发布于 2015-08-03 23:40:48
找到了解决办法,花了一段时间。
for (int x = 0; x < beastStatusArray.count; x++) {
NSNumber *indexNumber = [shuffleArrayIndex objectAtIndex:x];
NSNumber *randNumber = [shuffleArrayRand objectAtIndex:x];
NSUInteger indexNumb = [indexNumber integerValue];
NSUInteger indexRand = [randNumber integerValue];
[mainArray exchangeObjectAtIndex:indexNumb withObjectAtIndex:indexRand];
}https://stackoverflow.com/questions/31797964
复制相似问题