Add array to second column of another array

I'm new at Objective C. I have a list of questions, and each question has multiple answers, so I need to have an array which contains another array with a question number, then an array of answers.

I have an NSMutableArray which I'm instantiating it with this line:

_randomQuestionNumberArray = [[NSMutableArray alloc] initWithCapacity:2];

I fill this array with numbers:

for (int i = 0; i < numberOfQuestions; i++) {
    NSNumber* xWrapped = [NSNumber numberWithInt:i];
    [_randomQuestionNumberArray addObject:xWrapped];

Then I have another NSMutableArray which is just a regular array of numbers. I want to add this array to the second column of _randomQuestionNumberArray at a specific row. I'm using this code for this but it doesn't seem to work properly.

[_randomQuestionNumberArray insertObject:_tempAnswers atIndex:position];

Can anyone offer solution to this? Thank you!


Don't use an array of arrays. It will work initially, but it isn't flexible or clear (so hard to maintain).

Instead, use an array of dictionaries, where each dictionary has a number of keys:

_randomQuestionNumberArray = [NSMutableArray array];

for (int i = 0; i < numberOfQuestions; i++) {
    [_randomQuestionNumberArray addObject:[@{ @"questionNumber" : @(i) } mutableCopy]];

then, when you have your answers:

NSMutableDictionary *questionDict = [_randomQuestionNumberArray objectAtIndex:...];
[questionDict setObject:_tempAnswers forKey:@"answers"];

and now it's obvious what each piece of information is for.

Note: the index of each item in the array could work as the question number if you wanted it to...

Try using this: - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

in place of anObject put your array.

Need Your Help

Named pipes versus COM for IPC

performance com ipc named-pipes

Are named pipes faster than using COM (out-of-proc) for inter-process communication?

Application.Capturescreenshot(fileName) save to specific path

unity3d screenshot

i am using in my unity project this method that saves the camera viewport to the file system: