Save and load one value, iPhone

I have a feeling that there is an easy way to do this. I have a number from 1 to 100, and it is stored in a variable called firstValue. Then this variable is placed in UILabel. When the user exits my application, I want this firstValue to be saved, so that when the user loads the application, this number can be reinserted into UILabel (firstValueLabel.text).

Many thanks,

Stewart

+4
source share
2 answers

You can use NSUserDefaults for this.

To save it:

NSInteger myValue = ...; // This is your number NSString *key = ... ; // This is a string to identify your number [[NSUserDefaults standardUserDefaults] setInteger:myValue forKey:key]; 

To read this:

 NSInteger myValue = [[NSUserDefaults standardUserDefaults] integerForKey:key]; 

If the previous value was not saved, the return value will be 0.

Add this to your WillTerminate app:

 [[NSUserDefaults standardUserDefaults] synchronize]; 
+14
source

you can use NSUserDefaults to store some variable for your application to get them after the application closes. For instance:

 // Set the name of the view [[NSUserDefaults standardUserDefaults] setInteger:10 forKey:@"firstValue"]; 

To get the last user action:

 // Retrieve the last view name Integer firstValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"firstValue"]; 

But you can add not an integer. (see Link to NSUserDefaults class )

+3
source

Source: https://habr.com/ru/post/1301972/


All Articles