NSUserDefaults 在使用应用程序时保存并重新加载,但在应用程序重新启动时重置
NSUserDefaults save and reload while using app but reset when app is restarted
我有一种方法可以在特定日期第一次向用户发送消息时增加计数器。代码如下:
//here we decide if to increment it or not
-(BOOL)canIncrementCountForUser: (NSString *)user {
//erase the dictionary if it's a new day
[self flushDictionaryIfNeeded];
//load up a dictionary
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
NSLog(@"%@", [dictionary allKeys]);
//if empty it's a yes
if([dictionary count]==0){
NSLog(@"empty dictionary");
NSLog(@"First message for %@ today!",user);
NSDate *now = [NSDate date];
[dictionary setObject:now forKey:user]; //do I need to set it back again?
[defaults synchronize];
return YES;
}
//if it's not empty it's only a yes if the key doesn't exist
else {
//not in dict so unique
if(![dictionary objectForKey:user]){
NSLog(@"First message for %@ today!",user);
NSDate *now = [NSDate date];
[dictionary setObject:now forKey:user]; //do I need to set it back again?
[defaults synchronize];
return YES;
}
else {
NSLog(@"Already messaged %@ today!",user);
return NO;
}
}
}
-(void)flushDictionaryIfNeeded{
//set dictionary
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
if([dictionary count]>0) {
//get any date
NSDate *aDate = nil;
NSArray *values = [dictionary allValues];
aDate = [values objectAtIndex:0];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:aDate];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
if([today day] == [otherDay day] &&
[today month] == [otherDay month] &&
[today year] == [otherDay year] &&
[today era] == [otherDay era]) {
NSLog(@"Don't flush");
}
else {
NSLog(@"It's a new day! erase dictionary!");
[dictionary removeAllObjects];
[defaults synchronize];
}
}
}
如果向用户发送消息,将创建一个 NSDate 对象,并以用户的用户名作为键。如果密钥不存在,则可以递增计数器并添加密钥,如果它确实存在,则方法 returns false。如果是新的一天,我还有一种方法可以删除所有内容。代码一切正常,当程序正在使用时,NSMutableDictionary 似乎被保存到应用程序中,但是当我重新启动应用程序时,字典将为空。有人可以告诉我为什么会发生这种情况吗?谢谢
您修改了 dictionary
但从未退回 defaults
...您需要:
[defaults setObject:dictionary forKey:@"uniqueSentToday"];
保存默认值。
您需要将字典存储回用户默认值,因为您收到的字典是不可变的。尽管您将其设置为可变的,但更改不会反映在用户默认的原始存储字典中。
我有一种方法可以在特定日期第一次向用户发送消息时增加计数器。代码如下:
//here we decide if to increment it or not
-(BOOL)canIncrementCountForUser: (NSString *)user {
//erase the dictionary if it's a new day
[self flushDictionaryIfNeeded];
//load up a dictionary
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
NSLog(@"%@", [dictionary allKeys]);
//if empty it's a yes
if([dictionary count]==0){
NSLog(@"empty dictionary");
NSLog(@"First message for %@ today!",user);
NSDate *now = [NSDate date];
[dictionary setObject:now forKey:user]; //do I need to set it back again?
[defaults synchronize];
return YES;
}
//if it's not empty it's only a yes if the key doesn't exist
else {
//not in dict so unique
if(![dictionary objectForKey:user]){
NSLog(@"First message for %@ today!",user);
NSDate *now = [NSDate date];
[dictionary setObject:now forKey:user]; //do I need to set it back again?
[defaults synchronize];
return YES;
}
else {
NSLog(@"Already messaged %@ today!",user);
return NO;
}
}
}
-(void)flushDictionaryIfNeeded{
//set dictionary
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dictionary = [defaults objectForKey:@"uniqueSentToday"];
if([dictionary count]>0) {
//get any date
NSDate *aDate = nil;
NSArray *values = [dictionary allValues];
aDate = [values objectAtIndex:0];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:aDate];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
if([today day] == [otherDay day] &&
[today month] == [otherDay month] &&
[today year] == [otherDay year] &&
[today era] == [otherDay era]) {
NSLog(@"Don't flush");
}
else {
NSLog(@"It's a new day! erase dictionary!");
[dictionary removeAllObjects];
[defaults synchronize];
}
}
}
如果向用户发送消息,将创建一个 NSDate 对象,并以用户的用户名作为键。如果密钥不存在,则可以递增计数器并添加密钥,如果它确实存在,则方法 returns false。如果是新的一天,我还有一种方法可以删除所有内容。代码一切正常,当程序正在使用时,NSMutableDictionary 似乎被保存到应用程序中,但是当我重新启动应用程序时,字典将为空。有人可以告诉我为什么会发生这种情况吗?谢谢
您修改了 dictionary
但从未退回 defaults
...您需要:
[defaults setObject:dictionary forKey:@"uniqueSentToday"];
保存默认值。
您需要将字典存储回用户默认值,因为您收到的字典是不可变的。尽管您将其设置为可变的,但更改不会反映在用户默认的原始存储字典中。