将数组添加到另一个数组的第二列

时间:2022-10-28 16:47:48

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.

我是目标C的新手。我有一个问题列表,每个问题有多个答案,所以我需要一个包含另一个带有问题编号的数组的数组,然后是一系列答案。

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

我有一个NSMutableArray,我用这一行实例化它:

_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.

然后我有另一个NSMutableArray,它只是一个常规的数字数组。我想将此数组添加到特定行的_randomQuestionNumberArray的第二列。我正在使用此代码但它似乎无法正常工作。

[_randomQuestionNumberArray insertObject:_tempAnswers atIndex:position];

Can anyone offer solution to this? Thank you!

任何人都可以提供解决方案吗?谢谢!

2 个解决方案

#1


1  

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...

注意:如果您想要它,数组中每个项目的索引可以作为问题编号使用...

#2


0  

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

尝试使用: - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

in place of anObject put your array.

代替anObject放置你的数组。

#1


1  

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...

注意:如果您想要它,数组中每个项目的索引可以作为问题编号使用...

#2


0  

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

尝试使用: - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

in place of anObject put your array.

代替anObject放置你的数组。