Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 关于字符串和NSRangeException的简单NSSArray_Iphone_Objective C_Nsstring_Nsarray - Fatal编程技术网

Iphone 关于字符串和NSRangeException的简单NSSArray

Iphone 关于字符串和NSRangeException的简单NSSArray,iphone,objective-c,nsstring,nsarray,Iphone,Objective C,Nsstring,Nsarray,我有一个简单的问题。我正在设计一些简单的高分代码,但我有很多麻烦,特别是存储和保存球员的名字。我在obj-c中编程的时间不长,只有2周。我不太清楚在数组中使用NSString时发生了什么 以下是保存字符串的代码: -(NSString *) getFilePath2 { NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); retu

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

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

-(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时,需要记住哪些区别?

确保playerName1不为NULL