2014-01-18 3 views
0

필자는 텍스트 파일에서 열 데이터를 변환하고 배열 배열을 반환하는 클래스 메서드를 작성했습니다 ... 비어있는 배열의 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; 
} 

나는있는 NSString 또는의 NSNumber 중 하나가 teamData 배열의 마지막에 추가되고 있지만 leagueDatateamData를 추가 할 때 빈 개체가 추가되었는지 확인했습니다. 내가 뭘 놓치고 있니? 사전에

감사합니다, 루프에 브래드

+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

감사합니다 ... 나는 당신의 답변을 투표하려고 시도했지만 아직 그것에 대한 충분한 포인트가 없습니다. 나는 당신의 대답을 "받아 들였"습니다. –

관련 문제