0

我有一个简单的问题。我正在设计一些简单的高分代码,但我遇到了很多麻烦,特别是存储和保存玩家姓名。我在 obj-c 中编程的时间不长,只有 2 周。我不太确定在数组中使用 NSStrings 时发生了什么。

下面是保存字符串的代码:

-(NSString *) getFilePath2 {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"savedFile2.plist"];


}

-(void) savePlayerNameData {
    NSArray *highScoreNames = [[NSArray alloc]
                                initWithObjects:

                                playerName1,

                                playerName2,

                                playerName3,

                                playerName4,

                                playerName5,

                                nil];


    [highScoreNames writeToFile:[self getFilePath2] atomically:YES];

}

-(void) loadPlayerNameData{
    // load data here
    NSString *myPath = [self getFilePath2];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

    if (fileExists) {
        NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
        playerName1 = [values objectAtIndex:0];
        playerName2 = [values objectAtIndex:1];
        playerName3 = [values objectAtIndex:2];
        playerName4 = [values objectAtIndex:3];
        playerName5 = [values objectAtIndex:4];




    }
    else {
        NSLog(@"first Launch. no file yet");
    }
}

我最终得到这个错误:

2012-08-15 22:25:30.780 Pig Fly![5304:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
*** First throw call stack:
(0x14c5022 0xec5cd6 0x146da48 0x146d9b9 0x14bec30 0xbd21 0x62e2 0x964eb6 0x1499936 0x14993d7 0x13fc790 0x13fbd84 0x13fbc9b 0x13ae7d8 0x13ae88a 0x28626 0x1fdd 0x1f45)
terminate called throwing an exception(lldb) 

但是,我发现非常有趣的是,当我编写类似的代码来保存 int 值时,程序运行良好。这是工作的 int 代码:

-(NSString *) getFilePath {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"savedFile.plist"];
}


-(void) saveData {
    //save data here
    NSArray *highScores = [[NSArray alloc]
                           initWithObjects:

                           [NSString stringWithFormat:@"%i",highScore1],

                           [NSString stringWithFormat:@"%i",highScore2],

                           [NSString stringWithFormat:@"%i",highScore3],

                           [NSString stringWithFormat:@"%i",highScore4],

                           [NSString stringWithFormat:@"%i",highScore5],

                            nil];


    [highScores writeToFile:[self getFilePath] atomically:YES];



}

-(void) loadData{
    // load data here
    NSString *myPath = [self getFilePath];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

    if (fileExists) {
        NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
        highScore1 = [[values objectAtIndex:0] intValue];
        highScore2 = [[values objectAtIndex:1] intValue];
        highScore3 = [[values objectAtIndex:2] intValue];
        highScore4 = [[values objectAtIndex:3] intValue];
        highScore5 = [[values objectAtIndex:4] intValue];




    }
    else {
        NSLog(@"first Launch. no file yet");
    }
}

谁能告诉我发生了什么事?在为 int 和 NSString 编写 NSArray 时需要牢记哪些区别?

4

1 回答 1

0

确保 playerName1 不为 NULL。

于 2012-08-16T02:50:33.787 回答