2015-08-03 4 views
0

У меня есть массив под названием mainArray. Скажем, он содержит Red, Orange, Yellow, Green, Blue.Shuffle массив с exchangeObjectAtIndex Loop?

Я хочу изменить порядок mainArray, используя значения из двух других массивов, shuffleArrayIndex и shuffleArrayRand.

[mainArray exchangeObjectAtIndex:x withObjectAtIndex:randInt];

Значение индекса x, будет исходить от shuffleArrayIndex. Скачан shuffleArrayIndex Содержит 1, 3, 4, 2, 0.

СOOjectAtIndex randInt будет получен из shuffleArrayRand. Скачан shuffleArrayRand Содержит 2, 1, 3, 4, 1.

Так что я хочу, чтобы создать цикл, который будет обмениваться 'объекты S 5 раз (mainArray' в mainArray s .count)

Таким образом, первый обмен будет выглядеть следующим образом:

[mainArray exchangeObjectAtIndex:1 withObjectAtIndex:2];

И Второе:

[mainArray exchangeObjectAtIndex:3 withObjectAtIndex:1];

И третье:

[mainArray exchangeObjectAtIndex:4 withObjectAtIndex:3];

Так как я могу создать цикл, который делает это, что будет обменивать столько раз, сколько mainArray.count?

Так что, если mainArray имеет 50 объектов, а shuffleArrayIndex и shuffleArrayRand есть 50 объектов, цикл обменяет mainArray «s объекты в 50 раз.

+0

Вы пробовали что-нибудь? Вы можете показать этот код – Paulw11

ответ

0

Нашли решение, потребовалось некоторое время.

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]; 
       } 
Смежные вопросы