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

  • Home
  • SEARCH
  • 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 6659989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:05:56+00:00 2026-05-26T02:05:56+00:00

I am trying to increment a number in a simple way that won’t cause

  • 0

I am trying to increment a number in a simple way that won’t cause duplicates or missed numbers. This number will eventually be stored in a field in my coreData entity as an order number which is why I can’t have duplicates or missed numbers.

I have a view for entering orders and when it initialises it must provide the correct order number, this is what I have coded:

-(void) viewDidLoad {
 // If no orders have previously been entered the order number will be changed from 0 to 1
          if (orderNumber == 0) {
          orderNumber = orderNumber + 1;
          }
// If orders have previously been entered the order number will be given the value that has previously been stored in orderNumber
          else orderNumber = orderNumber;


NSLog(@"The Order Number is %d",orderNumber); 
}

When the form on the view has been completed a done button is pressed, this finalises the order. All textfields on the view are cleared and the order number is increased by 1 so that the next order can be entered.

-(void) doneButtonTapped:(id)sender{
   orderNumber = orderNumber + 1;
   NSLog(@"orderNumber is increased to %d",orderNumber);
}

My header file includes the following:

int orderNumber;
@property (nonatomic,assign) int orderNumber;

This works while the application is running but if I quit the application and re-run it my orderNumber starts back at 1.

How can I store/save the value associated to orderNumber for use in viewDidLoad when the application runs again?

Is my method for incrementing ok or is there a better way? I have used int but I see others use NSNumber, I am not sure if there are advantages & disadvantages of using one or the other.

Any help/suggestions would be 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-05-26T02:05:57+00:00Added an answer on May 26, 2026 at 2:05 am

    You can use NSUserDefaults to store ints and other basic types (BOOL, float, NSString, NSArray).

    For example, you can retrieve the int using

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    int orderNumber = [prefs integerForKey:@"orderNumber"];
    

    and then save the int using

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:orderNumber  forKey:@"orderNumber"];
    

    When necessary, you can also call

       [prefs synchronize];
    

    to ensure that you are accessing the most up to date information. This is especially useful if you are accessing this int in multiple places in your app.

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

Sidebar

Related Questions

I'm trying to build a simple Android app that increments a number displayed every
I'm trying to write a simple program that will compare the files in separate
I am trying to create this simple GUI where the number of clicks is
I am trying to increment the z-index of each list item. This is what
I am trying to increment a value (in this case a UNIX timestamp, but
I am trying to find a way to increment a second primary key column
I am trying a simple Extension Method example and am unable to increment or
Just trying to do a (I thought) simple callout to read a number from
I'm trying to write a simple utility method for adding aninteger number of days
I'm trying to set up a very simple UDP socket server with python that

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.