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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:16:39+00:00 2026-06-09T21:16:39+00:00

I need to save a variable that increments every time a user hits a

  • 0

I need to save a variable that increments every time a user hits a button within the app. I’ve been trying to do that with NSUserDefaults with no success. Here’s the code I’m using:

[[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:@"AppCount"];

When I output this in the log, however, I keep getting 0.

Any help would be greatly appreciated.

  • 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-09T21:16:41+00:00Added an answer on June 9, 2026 at 9:16 pm

    Try adding:

    [[NSUserDefaults standardUserDefaults] synchronize];
    

    After each time you change the value of your integer.

    Quoting Apple’s documentation on synchronize:

    Writes any modifications to the persistent domains to disk and updates
    all unmodified persistent domains to what is on disk.

    http://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/synchronize

    So with this modification, your final code should look like this:

    [[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:@"AppCount"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    EDIT: Actually on second thought, give this a try:

    I think the problem is using i++ assumes that the application will always be able to keep track of the count, and every time you re-enter the application i gets reset.

    The following works, I just tested it.

    if (![[NSUserDefaults standardUserDefaults] integerForKey:@"AppCount"]) {
        [[NSUserDefaults standardUserDefaults] setInteger:i forKey:@"AppCount"];
    }else{
        [[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:@"AppCount"] + 1 forKey:@"AppCount"];
    }
        [[NSUserDefaults standardUserDefaults] synchronize]; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to save to an isolated storage the total time that the user
I have a simple Perl regex that I need to save as a variable.
I need to save objects - instances of classes - in my app like
So in Android/Java, I need to access a global variable that is an EditText[]
I need to have a UserProfile class that it's just that, a user profile.
I need to save the output of this command into a variable $scriptName |
I need to save the barcode with timestamp and save that in a .csv
I have a series of bitmap images that I need to save using .NET
.txt file need save in SD-card. Save(n + String.valueOf(key) + .txt); private void Save(String
(sorry for my english) I need save an image in the application path and

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.