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.

4

2 回答 2

5

NSUserDefaults是要走的路。

NSString * yourKey = @"someKey";
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[defaults integerForKey:yourKey] + 1 forKey:yourKey]
于 2012-02-22T21:36:28.097 回答
0

将其保存在NSUserDefaults中。NSUserDefaults 易于学习;你只需要几行代码。尝试使用两个变量:

UInt32 count
UInt32 fourBillion

由于 UInt32 的最大值略大于 40 亿,您可以递增 count 直到达到 40 亿,然后为下一次递增,将 count 设置为 0 并递增 4Billion。

然后要获得真正的计数,乘以fourBillion4,000,000,000,然后加上count。请务必使用可以存储最大可能值的数据类型。这使您可以存储数量惊人的数字;可能远远超过你的需要。

于 2012-02-22T21:39:47.490 回答