2011-08-09 64 views
0

我正在嘗試創建一個多選測試應用程序。我有這樣的代碼來讀出一個.txt文件:幫助分組數組索引(objectAtIndex 0 - 9)= 1索引點

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是新的可變數組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]; 
     } 

的,對於循環的結果是,questionGroupArray objectAtIndex:0包含問題1和問題10.什麼是實現我的目標,使questionGroupArray索引0包含「問題1」,以最好的方式之間的一切「[空間1]」?我覺得有一種方法可以用for循環來做到這一點,但它逃脫了我。謝謝!

回答

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

有沒有辦法把這個500萬點?非常感謝你,這很有魅力。可能會花上幾天的時間。 – JustAnotherCoder

+0

不是我所知道的。但另一個快樂的iOS開發讓我開心。 – PeyloW

0

在每次迭代中爲指針qArray創建一個新陣列。現在您只需將多個相同的陣列添加到questionGroupArray

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

看着這個很快我相信這也會奏效。 ty – JustAnotherCoder

+0

Offcourse它會,但PeyoloW有更好的解決方案。 –