NSUserDefaults 奇怪的行为 - 改变存储的值

NSUserDefaults strange behaviour - changing stored values

我正在构建一个用于点击计数的应用程序(例如:进入房间的人数)。主要目标是能够拥有多个计数器(例如:主房间计数器、白色房间计数器、绿色房间计数器等)。

该应用包括:

1) 计数器列表-CounterListTableViewController,功能是在列表

中创建新项目(计数器)

2) 一个计数器页面 - RowCounterViewController

问题 #1:在 RowCounterViewController 中,计数值存储到 NSUserDefaults。在计数器中点击 3 次后,return 通过导航栏按钮 "back" 进入列表,然后返回该计数器,在 UILabel 中显示 3。到目前为止,一切都很好,但是如果我 return 到列表的次数超过 1 次,该值就会开始变化。发生了什么事?

这是上面解释的情况的NSLog:

2015-01-02 23:51:56.243 RowCounter[8522:380477] {
    0 = 0;
    1 = 0;
    2 = 0;
}
// here I tap 3 times in the counter with index "0" in the list

2015-01-02 23:52:07.184 RowCounter[8522:380477] {
    0 = 3;
    1 = 0;
    2 = 0;
}
// I return to the list and go back to the same counter, 3 times is there

2015-01-02 23:52:11.594 RowCounter[8522:380477] {
    0 = 0;
    1 = 0;
    2 = 0;
}
// I return again to the list and again back to the same counter, it now shows 0 (but should be 3)

2015-01-02 23:52:17.902 RowCounter[8522:380477] {
    0 = 0;
    1 = 0;
    2 = 0;
}
// I now go to the counter with index "1" and tap 5 times

2015-01-02 23:52:27.591 RowCounter[8522:380477] {
    0 = 0;
    1 = 5;
    2 = 0;
}
// I return to the list and go to a different counter with index "2", in the counter with index "1" count is 5

2015-01-02 23:52:30.401 RowCounter[8522:380477] {
    0 = 0;
    1 = 5;
    2 = 0;
}
// I again return to the list and again go to a different counter with index "2", in the counter with index "1" count is still 5

2015-01-02 23:52:34.029 RowCounter[8522:380477] {
    0 = 0;
    1 = 5;
    2 = 0;
}
// I again return to the list and again go to a different counter with index "2", in the counter with index "1" count is still 5 - So if not returning to the counter with count >0 it holds that count

2015-01-02 23:52:37.144 RowCounter[8522:380477] {
    0 = 0;
    1 = 0;
    2 = 0;
}
// I return to the list and go to the counter with index "1" and count 5 clears.

问题#2:这可能与第一个问题有关,按下主页按钮时我正在丢失计数,但由于无论如何我都在丢失计数,所以如果第一个问题得到解决,这可能会得到解决。

我存储和加载视图的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationDidEnterBackgroundNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationWillTerminateNotification object:nil];

    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
    NSLog(@"%@", dict);

    NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
    NSInteger numCounter = [[dict objectForKey:key] intValue] ;
    if (numCounter) {
        // counter = [numCounter intValue] ;
        count.text = [NSString stringWithFormat:@"%ld", (long)numCounter] ;
    } else {
        numCounter = 0 ;
        count.text = @"0" ;
    }
}


- (void)saveCount {
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
    NSMutableDictionary *mDict = nil ;
    if (dict == nil)
        mDict = [NSMutableDictionary dictionary] ;
    else
        mDict = [dict mutableCopy] ;

    NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
    [mDict setObject:@(counter) forKey:key] ;
    [[NSUserDefaults standardUserDefaults] setObject:mDict forKey:@"saveCount"] ;
    [[NSUserDefaults standardUserDefaults] synchronize] ;
}

感谢您的帮助!

这行似乎不正确:

NSInteger numCounter = [[dict objectForKey:key] intValue] ;
if (numCounter) {

您正在检查 numCounter 是否等于 1

我猜应该是

NSInteger numCounter = [[dict objectForKey:key] intValue] ;
if (numCounter > 0) {

不过话又说回来,您可以将整个内容重写为

NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex];
NSNumber* numCounter = dict[key];
if(!numCounter) 
    numCounter = @0;

count.text = [numCounter stringValue];

已发现问题:

我的评论行counter = [numCounter intValue] 报错 bad receiver type 'NSInteger' (aka "long")

最初我已经声明int counter 改为 NSInteger counter

取消注释上面的行并更改为:

NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
NSInteger numCounter = [[dict objectForKey:key] intValue] ;
if (numCounter > 0) {
    counter = numCounter;
    count.text = [NSString stringWithFormat:@"%ld", (long)numCounter] ;
} else {
    numCounter = 0 ;
    count.text = @"0" ;
}