开发者

How do I Increment Integer and save it for use when program is re-run?

开发者 https://www.devze.com 2023-04-12 04:46 出处:网络
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 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 initialise开发者_运维知识库s 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.


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.


-(void) viewDidLoad{

   NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

   //now take an int to store value

   int count = [userDefaults integerForKey:@"my_key"]; 
   // since we didn't stored any value for key "my_key" the above statement returns 0 by default

   //do your stuff by using that variable


  // now to increment and store the updated value ,we have to write following stuff

  [userDefaults setInteger:count+1 forKey:@"my_key"];

  [userDefaults synchronize];
}


You would use NSUserDefaults like so:

-(void) viewDidLoad {
 // If no orders have previously been entered the order number will be changed from 0 to 1
    int orderNumber = [[NSUserDefaults sharedDefaults] intForKey:@"orderNumber"];
    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;
    [[NSUserDefaults sharedDefaults] setInt:orderNumber ForKey:@"orderNumber"];
    [[NSUserDefaults sharedDefaults] synchronize];

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


-(void) doneButtonTapped:(id)sender{
   orderNumber = orderNumber + 1;
   [[NSUserDefaults sharedDefaults] setInt:orderNumber ForKey:@"orderNumber"];
   [[NSUserDefaults sharedDefaults] synchronize];
   NSLog(@"orderNumber is increased to %d",orderNumber);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号