我有两个NSUserDefaults键:第一个是与显示一个小介绍窗口相关的键,第二个是与前一个3个会话中的tableView中更改单元格背景颜色相关的键。
现在,当我更新我的应用程序时,我想要重置第二个键,以便为前3个会话显示不同的单元格背景颜色。
这是我的app delegate中的代码:
[[NSUserDefaults standardUserDefaults] setObject: @"1.3.5" forKey:@"lastVer"];//in memory
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSLog(@"**** Version: %@",version);
NSNumber *runNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"runNum"];
if(![lastVersion isEqualToString: version]){
NSNumber *runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:@"runNum"];
}
else {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:([runNumber intValue] + 1)] forKey:@"runNum"];
NSLog(@"This is Run Number: %d", [runNumber intValue]);
}
if (runNumber == [NSNumber numberWithInt:0]){
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:([runNumber intValue] + 1)] forKey:@"runNum"];
NSLog(@"Run number: %d", [runNumber intValue]);
}
(注意:runNumber是存储3个会话的单元格背景颜色的键)
它检查lastVersion是否等于bundle版本,并重置runNum的密钥(赋值为0)。然后,如果runNum为0,则会增加runNumbers。
但是有问题!
当lastVersion与版本不同时,它会重置密钥,但我不知道如何再次增加runNum,而不再重置运行次数,因为每次打开应用程序时都会重置runNum!
即
lastVersion = 1.3.4
version = 1.3.5
if lastVersion is different from version, then runNum == 0
and if runNum == 0, runNum = runNum + 1
//CLOSING AND REOPENING THE APP
runNum = 0
runNum = runNum +1
//CLOSING AND REOPENING THE APP
runNum = 0
runNum = runNum + 1
//and so on..
当我关闭并重新打开应用程序时,runNum键再次为0!
如何只重置runNum键一次,然后再将其递增?
修改
我做了以下更改:
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
// get the current bundle version.
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSLog(@"Last Version: %@, New Version: %@", lastVersion, currentVersion);
NSNumber *runNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"runNum"];
NSInteger runNumberValue = [runNumber integerValue];
if(![lastVersion isEqualToString: currentVersion]){
NSLog(@"Last Version and Version are not same.");
NSNumber *runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:@"runNum"];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"lastVer"];
NSLog(@"last Version is: %@",lastVersion);
}
else {
NSLog(@"Last Version and Version are same.");
NSInteger newRunNumberValue = runNumberValue++;
if(newRunNumberValue > 2) {
newRunNumberValue = 0;
}
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:newRunNumberValue] forKey:@"runNum"];
}
这是使前三个会话中的单元格背景发生变化的代码:
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastVer"];
// get the current bundle version.
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSLog(@"Last Version: %@, New Version: %@", lastVersion, currentVersion);
NSNumber *runNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"runNum"];
if (!runNumber) {
NSLog(@"**** Resetting Run Number to zero ****");
NSNumber *runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:@"runNum"];
} else {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:([runNumber intValue] + 1)] forKey:@"runNum"];
NSLog(@"This is Run Number: %d", [runNumber intValue]);
}
if(![lastVersion isEqualToString: currentVersion]){
NSLog(@"Last Version and Version are not same.");
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"lastVer"];
NSLog(@"last Version is: %@",lastVersion);
//What should i do here to make the updated app show the different cells background?
}
else {
NSLog(@"Last Version and Version are same.");
//What should i do here to make the updated app show the different cells background?
}
现在:当用户第一次下载(3个会话)和用户更新应用程序时(甚至在N个会话之后),如何让应用程序显示不同的单元格背景?我被困在这里。
有什么想法吗?
答案 0 :(得分:3)
我自己解决了!
if(![lastVersion isEqualToString: currentVersion]){
NSLog(@"Last Version and Version are not same.");
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"lastVer"];
NSLog(@"last Version is: %@",lastVersion);
NSNumber *runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:@"runNum"];
NSLog(@"runNumber is: %d",[runNumber intValue]);
}
else {
NSLog(@"Last Version and Version are same.");
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:([runNumber intValue] + 1)] forKey:@"runNum"];
NSLog(@" Run Number: %d", [runNumber intValue]);
}
答案 1 :(得分:2)
当最后一个版本不等于当前版本时,您应该重置运行编号。这将是您的新版本的第一次运行。因此,在将用户默认值更新为捆绑包中的版本号之前,请在此时重置运行编号。
答案 2 :(得分:0)
虽然你的问题不是很明确,但我只是想根据我能理解的内容提供帮助。请修改您的问题,以便更清楚地描述您的问题。
您可以记录NSString *版本的值。
即在之后:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
放一个
NSLog(@"version %@", version);
这应该确保你的
if([lastVersion isEqualToString: version]){
...
}
是否已执行。
此外,您无需从NSUserDefaults中删除密钥,只需更新它即可。您可以安全地从代码中删除此行:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"runNum"];
只需进行这些更改,并告诉我它是否有效。