1

I want to create a counter that is remembered for the life of the application, i.e. never forgotten, unless the application is uninstalled.

The counter is going to keep track of the number of requests the user makes... maybe I will look to reset it when it gets too big, but by and large it will just keep incrementing every time a request is made.

It needs to be of type UInt32. My main concern is: How do I save this value? I'm assuming that it's going to have to be saved in the plist. I have not had any experience with plists. I'm hoping someone might be able to supply some example code of how to save to the plist etc, and then maybe a tutorial link to working with plists. I am currently looking, but maybe someone has something they have had success with in the past.

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
C.Johns
  • 10,185
  • 20
  • 102
  • 156

2 Answers2

5

NSUserDefaults is the way to go.

NSString * yourKey = @"someKey";
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[defaults integerForKey:yourKey] + 1 forKey:yourKey]
rsez
  • 351
  • 2
  • 4
  • 3
    It might be worth chucking a `synchronize` in there too (or, better, doing one in your app delegate's `applicationWillResignActive:`), as otherwise you can lose data if the automatic periodic synchronise hasn't occurred between you making a change and your app being killed. – Tommy Feb 22 '12 at 21:39
  • okay cool, I am going to read up on NSUserDefaults.. thank you very much for the code example... I have heard about defaults but never looked into them. – C.Johns Feb 22 '12 at 21:39
  • Thanks Tommy, thats a good idea.. something i definatly would have overlooked. – C.Johns Feb 22 '12 at 21:40
  • Maybe it's worth mentioning that NSUserDefaults are synced to the iCloud by default now. So there may be a problem when a user has the app installed on two devices. – coverback Feb 22 '12 at 21:59
  • right, that might become a problem in the future.. So long as I know about it then I can sort it later on.. but I think this is still a solid solution moving forward. One question how can I check the value of my defaults.. is there a way to log this to the console? – C.Johns Feb 22 '12 at 22:30
  • 1
    @C.Johns You can log it out with `NSLog(@"%d", [[NSUserDefaults standardUserDefaults] integerForKey:yourKey]);` – rsez Feb 22 '12 at 23:38
  • ah, thats alot nicer I was using **NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);** which printed all of the different values in defaults. thanks for that. – C.Johns Feb 22 '12 at 23:49
0

Save it in NSUserDefaults. NSUserDefaults is easy to learn; you only need a few lines of code. Try using two variables:

UInt32 count
UInt32 fourBillion

Since UInt32 has a maximum value slightly more than 4 billion, you can increment count until it reaches four billion, then for the next increment, set count to 0 and increment fourBillion.

Then to get the true count, you multiply fourBillion by 4,000,000,000, then add count. Be sure to use a data type that can store the maximum possible value. This lets you store a staggeringly huge number; probably far more than you need.

bneely
  • 9,083
  • 4
  • 38
  • 46