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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:48:23+00:00 2026-05-11T23:48:23+00:00

(Problem solved now, see answers below) Hello, in my app I am trying to

  • 0

(Problem solved now, see answers below)

Hello, in my app I am trying to create a points counter. That increases a number by 10 every time a button is pressed.

I successfully managed to get my app to increase a value (0) by 1 every time a button is pressed, and to remember the latest value and display it next time the app starts. But when you press the button that’s suppose to increase the points value by 1 after the app restarts the number starts increasing from 0 not the remembered value.

How would I get the points to increase by 10 and remember the latest value and display (and count from it) after the application is exited and started again ?

Here is my current code:

Code for the button to increase the value by 1:

   - (IBAction)startClick:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"greeting"];

NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
counter.text = numValue;

viewDidLoad method:

   - (void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *greetName = [prefs stringForKey:@"greeting"];
NSInteger count = [prefs integerForKey:@"greeting"];
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}
  • 1 1 Answer
  • 1 View
  • 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-05-11T23:48:23+00:00Added an answer on May 11, 2026 at 11:48 pm

    There are a couple of things going on here. First off, you are saving count before you are incrementing it. That means even if the save was happening it would be one lower than you expect.

    The actual bug that you are seeing though is that you are eliding count. If you look you will notice that you have the following line in viewDidLoad:

    NSInteger count = [prefs integerForKey:@"greeting"];
    

    despite the fact that directly refer to count in startClick:. I presume you also have an ivar named count and that what is going on is you mean to set the ivar, but you are in fact setting the local variable. The ivar will be set to 0 by default when the class is alloced, which is why it always seems to be zeroed. You can fix it by changing that like to:

    count = [prefs integerForKey:@”greeting”];

    It is also possible your values are not being synched out to disk if you are crashing during application quit, but unless you are seeing crashes in the run log that should not be an issue. You can explicitly synchronize out the value if it is important.

    Are you compiling this code with warnings turned on? Are you trying to fix those warnings? If you have warnings turned on you should be seeing something like “local declaration of ‘count’ hides instance variable”

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

Sidebar

Related Questions

No related questions found

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.