开发者

NSMutablearray in NSMutablearray

I have a database with 26 movies and 3 questions per movie.

What I have done is my program randomly selects a question from the database and it's displayed on the screen. What I'm having trouble with is recording this question so that it doesn't get selected again.

So far I have been playing with this:

    usedMovie_id = [[NSMutableArray alloc] initWithCapacity:26];
    usedQuestion_id = [[NSMutableArray alloc] initWithCapacity:4];

    for (NSInteger i = 0; i < 26; ++i)
    {
        [usedMovie_id addObject:[NSNull null]];
    }
    for (NSInteger i = 0; i < 4; ++i)
    {
        [usedQuestion_id addObject:[NSNull null]];
    }
    while (i < 10) {
        int ii = [self genRandomNumberForMovies];
        int jj = [self genRandomNumberForQuestions:25];
        NSLog(@"ii %d, jj %d", ii,jj);
        [usedQuestion_id replaceObjectAtIndex:jj withObject:[NSNumber numberWithInt:jj]];
        [usedMovie开发者_StackOverflow社区_id replaceObjectAtIndex:ii withObject:usedQuestion_id];
        i++;
        //NSLog(@"movie_id array %@", usedMovie_id);

    }

My problem is that the array usedQuestion_id isn't different at any of the indexes in usedMovie_id, they're all the same. If usedQuestion_id has 1 and usedMovie_id is 13 in the first loop and then usedQuestion_id is 1, 3 and usedMovie_id is 10 in the second loop. The usedQuestion_id is updated to 1, 3 at the usedMovie_id at index 13 (from the first loop).

Also I need to record which question has been answered correct and incorrect so I only show the questions that haven't been answered correctly.

I don't want to have to create 26 nsmutablearrays each with their own data.

I'm thinking I may need a nsdictionary or even a database.. Also I could randomise at the beginning and loop through each question instead of randomising after each question has been answered.


If the list of movies and questions never changes, put them into an NSDictionary inside a .plist file and randomly draw from there. You could then store the question in a mutable array and check against that for duplicates. But you run the risk of extra processing going back for more random movies and questions that are all duplicates.

I do like your idea of randomizing the entire list of questions right off the bat, then display them sequentially. It would also give you the best chance to match the index of the array to correct/incorrect questions.

At the end of the day, using CoreData or SQLite db to store everything would probably be your most efficient option, but if you are not as experienced with them, or don't want the overhead, you could get creative with .plist file and arrays.


It does seem like you are getting into the realm where a database may be helpful, especially if at some point these 4 movies turn into 40 movies and thousands of questions. If you are willing to give it a try, Core Data can help a lot here.

It will also help prevent having to hardcode in all of those values as you can grab them dynamically from the database.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html


This is for anyone else that was in my position.

What I did was scrap the idea of randomising on-the-go. Instead I would create an array, place the rowids* (because my list of questions are in a database) into the array then shuffle** the list of rowids and place that list into an array. Which I can then use to link to a database and draw a question.

This solves the problem of repeating questions.

*rowids are where a particular question is located. you might have to create your own primary keys and stuff depending on how you organised your questions. but i found out that I could use the rowids. Eliminates the need for me to use a movie_id and then a question_id.

**Look up fisher-yates algorithm to shuffle. There are also examples on stackoverflow that people have answered with code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜