Равномерное распределение Objective-C от 5 разных NSMutableArrays - PullRequest
0 голосов
/ 09 июня 2018

Это мой код, но он все еще не верен.В настоящее время я могу распространять так: 0,1,2,0,1,2,0,1,2, но я буду распространять 0,1,2,2,1,0,0,1,2,2 (01,2 группы)

//Create x Groups
for (int z=0; z<numberOfGroups; z++) {
    mutableArrayOfSubarrays[z] = [NSMutableArray arrayWithCapacity:countOfElementsForTheGroups];
}
int nextSubarray = 0;

//Distribute the Objects into the groups
    for (int i = 0; i < [AllObjectsToDistribute count]; i++)
    {

      [mutableArrayOfSubarrays[nextSubarray] addObject:[AllObjectsToDistribute objectAtIndex:i]];
      nextSubarray = nextSubarray % customGroups;
      nextSubarray++;
      nextSubarray = nextSubarray  % customGroups;
        }

1 Ответ

0 голосов
/ 12 июня 2018

Следите за тем, как рассчитывается групповой индекс в BOOL.Пример:

NSUInteger groupIndex = 0;
BOOL groupIndexCountsUp = YES;
for (id object in allObjectsToDistribute) {
    [groupsArray[groupIndex] addObject:object];
    if (groupIndexCountsUp) {
        if (groupIndex < numberOfGroups - 1)
            groupIndex++;
        else
            groupIndexCountsUp = NO;
    }
    else {
        if (groupIndex > 0)
            groupIndex--;
        else
            groupIndexCountsUp = YES;
    }
}
...