帮助分组数组索引(objectAtIndex 0 - 9)= 1索引点

时间:2011-08-09 07:20:56

标签: iphone objective-c arrays for-loop text-files

我正在尝试创建一个多选测试应用。我有这段代码来读取.txt文件:

filePath = [[NSBundle mainBundle] pathForResource:@"testBank" ofType:@"txt"];
theBank = [[NSString alloc] initWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding error:NULL];        
multipleChoicePractice = [theBank componentsSeparatedByString:@"\n"];

multipleChoicePractice NSMutableArray现在按此顺序包含一堆NSStrings:

 Question 1
 choice A
 choice B
 choice C
 choice D
 Answer Key
 Rationale
 question ID 1
 [string to id type of question]
 [space 1]
 Question 2
 choice A-2 
 etc etc up to Question 10

我正在尝试将每个索引分组为10个组,以便multipleChoicePractice的索引0到9是新的可变数组questionGroupArary的索引0。我试过了:

for (i=0; i<=90; i = i+10) { //qArr being a NSMutableArray
            [qArr addObject:[multipleChoicePractice objectAtIndex:i]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+1)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+2)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+3)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+4)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+5)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+6)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+7)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+8)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+9)]];

            [questionGroupArray addObject:qArr];
        }

for循环的结果是questionGroupArray objectAtIndex:0包含问题1和问题10之间的所有内容。实现目标的最佳方法是什么,以便questionGroupArray索引0包含“问题1”到“[空格1]”?我觉得有一种方法可以用for循环来做到这一点,但它逃脱了我。谢谢!

3 个答案:

答案 0 :(得分:1)

在连续范围内获取对象子阵列的更好方法是使用-[NSArray subarrayWithRange:]

// Source is your ungrouped array
NSMutableArray* groups = [NSMutableArray array];
for (int i = ; i < 90; i += 10) {
    NSArray* sub = [source subarrayWithRange:NSMakeRange(i, 10)];
    [groups addObject:sub];
}
// groups is now an array of arrays with your groups.

答案 1 :(得分:1)

在循环内创建数组。

for (i=0; i<=90; i = i+10) { //qArr being a NSMutableArray

// Here you have to create a new array for every object
             qArr = [NSMutableArray array];
            [qArr addObject:[multipleChoicePractice objectAtIndex:i]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+1)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+2)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+3)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+4)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+5)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+6)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+7)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+8)]];
            [qArr addObject:[multipleChoicePractice objectAtIndex:(i+9)]];

            [questionGroupArray addObject:qArr];
        }

另一个优化是有两个for循环

for(i=0;i<90;i= i+10)
{
qArr = [NSMutableArray array];
for (j=0;i<10;j++)
{
[qArr addObject:[multipleChoicePractice objectAtIndex:j]];
}
}

答案 2 :(得分:0)

在每次迭代中为指针qArray创建一个新数组。现在您只需将同一个数组多次添加到questionGroupArray