帮助分组数组索引 (objectAtIndex 0 - 9) = 1 个索引点
我正在尝试创建一个多项选择测试应用程序。我有这段代码来读取 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每次迭代中为指针
qArray
创建一个新数组。现在,您只需将同一数组多次添加到questionGroupArray
中即可。Create a new array for the pointer
qArray
in each iteration. Now you are simply adding the same array to thequestionGroupArray
multiple times.获取连续范围内的对象子数组的更好方法是使用
-[NSArray subarrayWithRange:]
。A much better way to get a subarray of objects in a consecutive range is to use
-[NSArray subarrayWithRange:]
.在循环内创建数组。
另一个优化是有两个 for 循环
Create the array inside the loop.
Another optimisation is to have two for loops