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

发布于 2024-11-28 17:57:13 字数 1842 浏览 2 评论 0原文

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

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

multipleChoicePractice NSMutableArray 现在包含一堆按以下顺序的 NSString:

 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 是新索引的索引 0可变数组,questionGroupArary。我尝试过:

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 包含“question1”到“[space 1]”?我觉得有一种方法可以用 for 循环来做到这一点,但我没有想到。谢谢!

I am trying to create a multiple choice test app. I have this code to read off a .txt file:

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

The multipleChoicePractice NSMutableArray now contains a bunch of NSStrings in this order:

 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

I am trying to group each index in groups of 10 so that index 0 to 9 of multipleChoicePractice is index 0 of a new mutable array, questionGroupArary. I tried:

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];
        }

The result of that for loop is that questionGroupArray objectAtIndex: 0 contains EVERYTHING between question 1 and question 10. What is the best way to achieve my goal so that questionGroupArray index 0 contains "question1" to "[space 1]"? I feel like there is a way to do this with a for loop but it escapes me. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨洒年华 2024-12-05 17:57:14

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

Create a new array for the pointer qArray in each iteration. Now you are simply adding the same array to the questionGroupArray multiple times.

甜`诱少女 2024-12-05 17:57:13

获取连续范围内的对象子数组的更好方法是使用 -[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.

A much better way to get a subarray of objects in a consecutive range is to use -[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.
世界和平 2024-12-05 17:57:13

在循环内创建数组。

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]];
}
}

Create the array inside the loop.

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];
        }

Another optimisation is to have two for loops

for(i=0;i<90;i= i+10)
{
qArr = [NSMutableArray array];
for (j=0;i<10;j++)
{
[qArr addObject:[multipleChoicePractice objectAtIndex:j]];
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文