2012-09-30 47 views
2

我得到「存儲到'myKeyArray'的值在其初始化過程中永遠不會被讀取」標記線的消息,我不明白爲什麼會發生這種情況。存儲到'myKeyArray'在初始化過程中的值永遠不會被讀取

- (void)createAndMigrateStatisticalDataForPlayersToV3 { 

NSString *playerStatsfilePath = [self dataFilePath5]; 
NSString *playerDatafilePath = [self dataFilePath6]; 
NSArray *myKeyArray = [[NSArray alloc]init]; <<<< "Value stored to 'myKeyArray' during its initialization is never read" 
NSMutableArray *theObjects = [[NSMutableArray alloc]init]; <<<< "Value stored to 'myKeyArray' during its initialization is never read" 

NSFileManager *fileMgr; 
fileMgr = [NSFileManager defaultManager]; 

if ([fileMgr fileExistsAtPath: playerDatafilePath] == YES) { 

    NSMutableDictionary *gameFileDict = [NSMutableDictionary dictionaryWithContentsOfFile:playerDatafilePath]; 

    NSMutableArray *dataArray = [[NSMutableArray alloc]init]; 
    NSMutableArray *newDataArray = [[NSMutableArray alloc]init]; 

    myKeyArray = [gameFileDict allKeys]; 

    int nrOfKeys = [myKeyArray count]; 

    for (int oo = 0; oo < nrOfKeys; oo++) { 
     theObjects = [gameFileDict valueForKey:[myKeyArray objectAtIndex:oo]]; 
     [dataArray addObject:[myKeyArray objectAtIndex:oo]]; // Name 
     [dataArray addObject:[theObjects objectAtIndex:1]];  // # of games played 
     [dataArray addObject:[theObjects objectAtIndex:2]];  // # correct answers 
     [dataArray addObject:[theObjects objectAtIndex:3]];  // # questions 
     [dataArray addObject:[theObjects objectAtIndex:4]];  // # of games won 
     [dataArray addObject:[theObjects objectAtIndex:5]];  // # of games lost 
    } 

    int dataCount = [dataArray count]; 
    float avgNrOfQuestionsPerGame = 0; 
    float avgNrCorrectAnswersPerGame = 0; 
    float nrOfQuestions = 0; 
    float nrOfCorrectAnswers = 0; 
    float nrOfGamesPerPlayer = 0; 
    float nrOfMatchesWonPerPlayer = 0; 
    float nrOfMatchesLostPerPlayer = 0; 

    for (int oo = 0; oo < dataCount; oo = oo + 6) { 

     nrOfGamesPerPlayer = [[dataArray objectAtIndex:oo+1]floatValue]; 
     nrOfCorrectAnswers = [[dataArray objectAtIndex:oo+2]floatValue]; 
     nrOfQuestions = [[dataArray objectAtIndex:oo+3]floatValue]; 
     nrOfMatchesWonPerPlayer = [[dataArray objectAtIndex:oo+4]floatValue]; 
     nrOfMatchesLostPerPlayer = [[dataArray objectAtIndex:oo+5]floatValue]; 

     //   nrOfGamesPerPlayer = nrOfMatchesLostPerPlayer + nrOfMatchesLostPerPlayer; 
     avgNrOfQuestionsPerGame = nrOfQuestions/nrOfGamesPerPlayer; 
     avgNrCorrectAnswersPerGame = nrOfCorrectAnswers/nrOfGamesPerPlayer; 

     for (int ff = 0; ff < nrOfGamesPerPlayer; ff++) { 
      [newDataArray addObject:[dataArray objectAtIndex:oo]];  //PlayerName 
      [newDataArray addObject:[self get_the_Date]];    //set todays date 
      [newDataArray addObject:[NSNumber numberWithInt:avgNrOfQuestionsPerGame]]; 
      [newDataArray addObject:[NSNumber numberWithInt:avgNrCorrectAnswersPerGame]]; 


     } 
    } 
    [newDataArray writeToFile:playerStatsfilePath atomically: TRUE]; 
} 

回答

1

在爲它們分配內存後,您將重新分配myKeyArray和theObjects,因此您會發生大量內存泄漏。

與分配刪除的聲明和移動的變量聲明行,你執行任務:

NSArray *myKeyArray = [gameFileDict allKeys]; 

同爲theObjects:

NSArray *theObjects = [gameFileDict valueForKey:[myKeyArray objectAtIndex:oo]]; 

請注意,您不必將其聲明爲NSMutableArray。你並沒有改變陣列。

+0

我先回答了這個問題,但確定。 –

7

我想把這條線拿出來,你不需要初始化它。

//NSArray *myKeyArray = [[NSArray alloc]init]; 

更改爲

NSArray *myKeyArray; 

你只需要分配它,如果它是可變的。

+0

它應該分配在哪裏? – PeterK

+0

它不應該。 –

+0

好吧,只需將其更改爲「NSArray * myKeyArray;' 或在第一次使用時添加NSArray *。 –

相關問題