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 8863025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:52:21+00:00 2026-06-14T15:52:21+00:00

I am trying to save data when the home button is pressed. Here is

  • 0

I am trying to save data when the home button is pressed.
Here is my relevant code.

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

}
- (void)applicationDidEnterBackground:(UIApplication *)application {
     NSArray *values = [[NSArray alloc] initWithObjects:
                   [NSNumber numberWithInt:askedQuestions],  //for questionLabel
                   [NSNumber numberWithInt:timeMin],         //timeMin
                   [NSNumber numberWithInt:timeSec],         //timeSec
                   [NSNumber numberWithInt:skipCount],       //skipped question
                   [NSNumber numberWithInt:correctAnswers],   //corectAnswers
                   nil];
[values writeToFile:[self saveFilePath] atomically:YES];
 }

saveFilePath method is providing the file path.

And in viewDidLoad

NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath]; 
if (fileExists)
{
    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    _readerLabel.text = [[values objectAtIndex:0] stringValue];
    askedQuestions=[[values objectAtIndex:0] intValue];
    timeMin=[[values objectAtIndex:1]intValue];
    timeSec=[[values objectAtIndex:2]intValue];
    skipCount=[[values objectAtIndex:3]intValue];
    correctAnswers=[[values objectAtIndex:4]intValue];

}

UIApplication *myApp = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
 name:UIApplicationDidEnterBackgroundNotification
 object:myApp];

and i am getting this:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]:        index (0) beyond bounds (0)'
*** First throw call stack:
(0x16a1012 0x13aee7e 0x16a0deb 0x16957e0 0x6891 0x312d 0x305817 0x305882 0x254a25 0x254dbf 0x254f55     0x25df67 0x221fcc 0x222fab 0x234315 0x23524b 0x226cf8 0x2703df9 0x2703ad0 0x1616bf5 0x1616962 0x1647bb6   0x1646f44 0x1646e1b 0x2227da 0x22465c 0x26cd 0x25f5)
libc++abi.dylib: terminate called throwing an exception

Where is the problem in the code. Please help!

  • 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-14T15:52:22+00:00Added an answer on June 14, 2026 at 3:52 pm

    You’re not checking to see if anything has worked!

    What happens if

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    

    either fails (the file might not exist) or if it returns an array with 0 items in?

    You need to add something like :

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    if (values.count > 0) {
      ...
    }
    

    You should also set a breakpoint on applicationDidEnterBackground: and step though to make sure that your save method is working as you expect.


    Also, I suspect one of these two lines is wrong :

    _readerLabel.text = [[values objectAtIndex:0] stringValue];
    askedQuestions=[[values objectAtIndex:0] intValue];
    

    they’re both asking for the item at index 0

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

Sidebar

Related Questions

I'm trying to save simple text data in my app using this code from
I am trying to save data to a database on a button push, but
I am trying to save data in the documents folder on iPhone 5.1 simulator.
I am trying to save data into the same table several times from the
I'm trying to create core data application. Some times when trying to save data,
I'm trying to save some data into an array but unfortunately all the data
I am trying to save my data from a view that was loaded on
I'm trying to save some data to A file in android, I'm using herefor
I am trying to save some data for a android game, but I cant
I'm trying to save the returned data from HTTP request into a variable. The

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.