Search code examples
objective-carrayscore-dataenumerator

how to get objects from two arrays?


I want make some quizes about movie.

There is quiz mutable array with 10 objects. Using core data..entity is movie. attributes are title, actor and correctcount.

There are two textfields,

Titlefield and actorfield.

I want to make two quizes from one object

First quiz which titlefield is hidden, Second quiz which actorfield is hidden.

and user can type the movie title or actor name. in textfields.

And if user type correct thing, i want to +1 correct count.

What i did is..

i put the quiz mutable array into these two arrays

Array A (quiz 1 to 10) <<for title quiz
Array B (same as array A) <<for actor quiz

And Movie *quizMovie

And.. after suffle these two arrays Set enumerators to use nextObject method.

NSEnumerator *enu1 = [arrayA objectEnumerator];
NSEnumerator *enu2 = [arrayB objectEnumerator];

Set number =1 <

And UIButton to set quiz and start the quiz

If( number%2==1)
quizMovie = [enu1 nextObject]
Titlefield.hidden = YES;
else
quizMovie = [enu2 nextObject]
Actorfield.hidden = YES;

And when user tap the done button, If correct, i want to +1 correctcount.

With these codes.. I can't get what i want...

Complecate..

Help me, Geniuses!!


Solution

  • Combine, then use a sorting method:

    NSArray *combinedArray = [arrayA arrayByAddingObjectsFromArray:arrayB];
    NSSortDescriptor *ascInts = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:ascInts];
    combinedArray = [combinedArray sortedArrayUsingDescriptors:sortDescriptors];
    

    This is assuming the members are instances of NSNumber.

    edit This method selects from one array if the press is odd, a different if it's even, as I think you desire based on our conversations.

    static int pressNumber = 0;
    pressNumber++;
    NSArray *sourceArray;
    if(pressNumber%2) { // if it's an odd-numbered press
      sourceArray = arrayA;
    } else {
      sourceArray = arrayB;
    }
    int indexToSelect = (int) (floor(pressNumber/2.0))+1;
    if(indexToSelect >= sourceArray.count) {
      // out-of-bounds
      pressNumber = 0;
      indexToSelect = 1;
    }
    selecteObject = [sourceArray objectAtIndex:indexToSelect];