只有第一个创建的对象可以保存到NSUserDefaults中的NSMutabledictionary

时间:2015-03-09 14:30:00

标签: ios xcode nsuserdefaults nsmutabledictionary

如果是第一次在特定日期向用户发送消息,我有一种增加计数器的方法。代码如下:

-(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 setObject:dictionary forKey:@"uniqueSentToday"];
        [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(@"username we are checking is: %@",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 setObject:dictionary forKey:@"uniqueSentToday"];
            [defaults synchronize];
            return YES;
        }
        else {

            NSLog(@"Already messaged %@ today!",user);
            //[dictionary removeAllObjects];
            //[defaults setObject:dictionary forKey:@"uniqueSentToday"];
            //[defaults synchronize];
            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 setObject:dictionary forKey:@"uniqueSentToday"];
            [defaults synchronize];
        }
    }
}

如果用户收到消息,将使用用户的用户名作为密钥创建NSDate对象。如果某个键不存在,则可以递增计数器并添加该键,如果确实存在,则该方法返回false。如果是新的一天,我还有一种方法可以删除所有内容。

我遇到了一个奇怪的问题。如果字典是空的并且我通过方法传递用户名它将正常工作并返回true,即可以递增计数器,以便将用户添加到字典中。它将保存此结果,如果我再次尝试传递该字符串,它将告诉我用户已按要求发送消息。但是,如果我在其中放入一个新字符串,代码将不会将新对象保存到字典中。我可以继续传递字符串,它表示用户尚未收到消息。如果我重新启动应用程序,然后再次尝试它将工作并将对象保存到字典,但之后使用的任何其他字符串将无法正常工作。它只会保存第一个用户名密钥和对象..任何人都可以给我任何指示,为什么会发生这种情况?这很奇怪。

1 个答案:

答案 0 :(得分:0)

按需运作。我添加了

dictionary = [NSMutableDictionary dictionaryWithCapacity:1];

 -(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 (!dictionary) {
        dictionary = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    // ...
}

- (IBAction)test1ButtonSelected:(id)sender {
    BOOL canIncrement = [self canIncrementCountForUser:@"test1"];
    if (canIncrement) {
        NSLog(@"yes, i can");
    }
    else {
        NSLog(@"can not");
    }
}