Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:37:52+00:00 2026-06-09T17:37:52+00:00

I have a simple question. I am designing some simple highscores code yet I

  • 0

I have a simple question. I am designing some simple highscores code yet I am having alot of trouble with it, specifically storing and saving players names. I have not been programming for very long in obj-c, only 2 weeks. I am not quite sure what is going on when I use NSStrings in an array.

Here is the code for saving strings:

-(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");
    }
}

I end up getting this error :

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) 

However, what I find very interesting is that when I write similar code for saving int values, The program runs fine. This is the working int code:

-(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");
    }
}

Can anybody tell me what is going on? What are the differences that need to be kept in mind when writing NSArrays for ints and NSStrings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:37:54+00:00Added an answer on June 9, 2026 at 5:37 pm

    Make sure that playerName1 is not NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some reading into designing template code have a question about it.
I have what is most likely a very simple question.. I am designing a
I have simple question. User supplies URL to my PHP script where I fetch
I have simple type Question : public class Question { public string[] Tags {
i have a simple question: i have this var: $v = 24000,1500,1500,1500,1500,1500,; i would
i have a simple question, there is a function with parameter emp_id that opens
I have a simple question: how exactly are array variables such as $_SESSION and
I have a simple question and that is how do you check if an
I have a simple question. I'm trying to get a list of the indicators
I have another simple question about Node. I'm trying to make a simple http

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.