PeterK
PeterK

Reputation: 4301

Value stored to 'myKeyArray' during its initialization is never read

I get the "Value stored to 'myKeyArray' during its initialization is never read" message for the marked lines and i do not quite understand why this is happening.

- (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];
}

Upvotes: 2

Views: 3377

Answers (2)

Rich Fox
Rich Fox

Reputation: 2194

I would take this line out, You shouldn't need to initialize it.

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

Change this to

NSArray *myKeyArray;

You only need to allocate it if it is mutable.

Upvotes: 7

Philippe Leybaert
Philippe Leybaert

Reputation: 171864

You're reassigning both myKeyArray and theObjects after you allocated memory for them, so you have a massive memory leak.

Remove the declarations with the allocations and move the variable declaration to the line where you perform the assignment:

NSArray *myKeyArray = [gameFileDict allKeys];

The same for theObjects:

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

Note that you don't have to declare it as NSMutableArray. You're not changing the array.

Upvotes: 1

Related Questions