2014-01-18 100 views
0

我已經編寫了一個類方法,用於轉換文本文件中的列數據並返回數組數組...只有它返回EMPTY數組的EMPTY數組。返回空數組的類方法

+(NSArray *) initArrayWithFileContents:(NSString *) theFilePath 
{ 
NSString *theContents = [self loadFile:theFilePath]; 
NSArray *theParagraphs = [self getParagraphs:theContents]; 

NSMutableArray *teamData = [[NSMutableArray alloc] init];  // array of team data 
NSMutableArray *leagueData = [[NSMutableArray alloc] init];  // array of arrays 

// set up number formatters for getting numbers from strings 
NSNumberFormatter *numberStyle = [[NSNumberFormatter alloc] init]; 
NSNumberFormatter *positiveNumberStyle = [[NSNumberFormatter alloc] init]; 
[numberStyle setNumberStyle:NSNumberFormatterDecimalStyle]; 
[positiveNumberStyle setNumberStyle:NSNumberFormatterDecimalStyle]; 
[positiveNumberStyle setPositiveFormat:@"'+'#"]; 

// set up a date and time formatter for getting time data from strings 
NSDateFormatter *timeStyle = [[NSDateFormatter alloc] init]; 
[timeStyle setDateStyle:NSDateFormatterNoStyle]; 
[timeStyle setDateFormat:@" mm:ss"]; 

for (NSString *currentParagraph in theParagraphs) 
{ 
    NSArray *currentTeam = [self getcolumnarData:currentParagraph]; // get an array of strings 
    for (NSString *currentItem in currentTeam) 
    { 
     NSNumber *currentStat = [numberStyle numberFromString:currentItem]; 
     if (currentStat != Nil) { 
      [teamData addObject:currentStat];   // number found 

     } else { 
      currentStat = [positiveNumberStyle numberFromString:currentItem]; 
      if (currentStat != Nil) { 
       [teamData addObject:currentStat];  // number with '+' sign found 

      } else { 
       NSDate *currentTime = [timeStyle dateFromString:currentItem]; 
       if (currentTime != Nil) { 
        NSNumber *theSeconds = [self calculateSeconds: currentTime]; 
        [teamData addObject:theSeconds]; // time found 

       } else { 
        [teamData addObject:currentItem]; // string found 
       } 
      } 
     } 
    } 
    [leagueData addObject:teamData]; // add child array to end of parent array 
    [teamData removeAllObjects];  // reset child array 
} 
NSArray *dataToReturn = [NSArray arrayWithArray:leagueData]; // convert to NSArray to return 
return dataToReturn; 
} 

以我的調試工作我已驗證被添加到所述teamData數組的末尾或者一個NSString或NSNumber的但添加teamDataleagueData當加入一個空對象。我錯過了什麼?

由於提前, 布拉德

+1

空數組的空數組沒有意義,第一個數組不是空的,因爲它包含一堆空數組。 –

回答

0

移動

NSMutableArray *teamData = [[NSMutableArray alloc] init]; 

進入死循環:

for (NSString *currentParagraph in theParagraphs) 
{ 
    NSMutableArray *teamData = [[NSMutableArray alloc] init]; 
    // ... 

    [leagueData addObject:teamData]; 
} 

循環。目前,您總是添加相同的陣列leagueData。 數組「僅」保留指向其元素的指針,因此最後leagueData的所有元素 都指向相同的數組teamData(從中刪除了所有對象,其中 )。

+0

Martin,感謝您的建議。在發佈問題後,我意識到自己在做什麼。我不習慣使用指針。我按照你的建議移動了'teamData'這一行,它的功能就像一個魅力。 –

+0

謝謝...我試圖給你的答案投票,但我還沒有足夠的積分。我現在已經「接受」你的答案。 –