简体   繁体   中英

Count and store a value in xcode

Hello everyone I would have a value for the need to save every time I press a button (count), virtually every time a user presses a button in my app should be pressed to keep count of the times (1,2,3,4, etc. etc.) the problem is that that data would be permanently saved, that is, even when you exit the app or restart the device should be saved on the iphone to find it and increase it reopened after the application. Is there an easier way to CoreData or SqlLite to do this?

thanks

Here is a sample code:

To store it:

    [[NSUserDefaults standardUserDefaults] setInteger: intCount forKey:@"Count"];
    [[NSUserDefaults standardUserDefaults] synchronize];

To read it:

    intCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"Count"];

The easiest way would be use NSUserDefaults to do this, no need for a bigger data store if it's only an integer.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

int buttonCount = 0;

// Do you count somewhere and add to buttonCount

[defaults setObject:[NSNumber numberWithInt:buttonCount] forKey:@"buttonPress"];

and to retrieve to number back just use

int buttonPress = [[[NSUserDefaults standardUserDefaults] objectForKey:@"buttonPress"] intValue];

NSUserDefaults is the solution. Update the stored value in every button click....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM