Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I Increment Integer and save it for use when program is re-run?
    primarykey
    data
    text
    <p>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.</p> <p>I have a view for entering orders and when it initialises it must provide the correct order number, this is what I have coded:</p> <pre><code>-(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); } </code></pre> <p>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.</p> <pre><code>-(void) doneButtonTapped:(id)sender{ orderNumber = orderNumber + 1; NSLog(@"orderNumber is increased to %d",orderNumber); } </code></pre> <p>My header file includes the following:</p> <pre><code>int orderNumber; @property (nonatomic,assign) int orderNumber; </code></pre> <p>This works while the application is running but if I quit the application and re-run it my orderNumber starts back at 1.</p> <p>How can I store/save the value associated to orderNumber for use in viewDidLoad when the application runs again?</p> <p>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 &amp; disadvantages of using one or the other.</p> <p>Any help/suggestions would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload