【问题标题】:Value stored to 'myKeyArray' during its initialization is never read在初始化期间存储到“myKeyArray”的值永远不会被读取
【发布时间】:2012-09-30 21:01:10
【问题描述】:

我收到标记行的“在初始化期间存储到 '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];
}

【问题讨论】:

    标签: objective-c memory-leaks


    【解决方案1】:

    我会把这条线去掉,你不需要初始化它。

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

    改成

    NSArray *myKeyArray;
    

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

    【讨论】:

    • 应该分配到哪里?
    • 好的,只需将其更改为NSArray *myKeyArray; 或在第一次使用时添加 NSArray *。
    • 我在答案中更改了它以明确,但如果它是可变的,您只需要分配它。
    【解决方案2】:

    在为 myKeyArray 和 theObjects 分配内存之后,您正在重新分配它们,因此存在大量内存泄漏。

    删除带有分配的声明并将变量声明移动到执行赋值的行:

    NSArray *myKeyArray = [gameFileDict allKeys];
    

    对象也一样:

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

    请注意,您不必将其声明为 NSMutableArray。你没有改变数组。

    【讨论】:

    • 我先回答了这个问题,但是还可以。
    猜你喜欢
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2018-07-26
    • 2015-07-26
    • 2012-01-19
    相关资源
    最近更新 更多