2

我为 NSUserDefaults 设置了一些值,如果我在不退出应用程序的情况下检索它们,一切正常。当我重新启动应用程序时,更改会在 95% 的时间内消失。在 5% 的时间内一切正常。

我第一次面对这个问题。在我的其他应用程序中,我通常不会将 NSString 保存在 NSUserDefaults 中。保存 NSString 的事实可能会导致问题吗?

这是我的代码:

    -(void) mayShowFeedbackMessage
    {
     NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
     [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
     [dateFormatter setDateStyle: NSDateFormatterMediumStyle];

     if ([ [NSUserDefaults standardUserDefaults] objectForKey: @"ratingsMessage_shown_on_date"])
     {
          NSString *lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                           objectForKey: @"ratingsMessage_shown_on_date"];
          NSDate *lastRatingDate = [dateFormatter dateFromString: lastRatingDateString];

          DebugLog(@"lastRatingDateString: %@", lastRatingDateString);
          DebugLog(@"time since last message: %.0f", [ [NSDate date] timeIntervalSinceDate: lastRatingDate]);

          if ( [ [NSDate date] timeIntervalSinceDate: lastRatingDate] >= 120.0f )
          {
              [self showFeedbackMessage];

              NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
              [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
              [dateFormatter setDateStyle: NSDateFormatterMediumStyle];

              NSString *dateString = [dateFormatter stringFromDate: [NSDate date] ];
             [dateFormatter release];

             [ [NSUserDefaults standardUserDefaults] setObject: dateString
                                                        forKey: @"ratingsMessage_shown_on_date" ];

             lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                     objectForKey: @"ratingsMessage_shown_on_date"];
             DebugLog(@"new date: %@", lastRatingDateString);

         }
     }
     else
     {  
         NSDateFormatter *dateFormatter = [ [NSDateFormatter alloc] init];
         [dateFormatter setTimeStyle: NSDateFormatterLongStyle];
         [dateFormatter setDateStyle: NSDateFormatterMediumStyle];

         NSString *dateString = [dateFormatter stringFromDate: [NSDate date] ];
         [dateFormatter release];

         [ [NSUserDefaults standardUserDefaults] setObject: dateString
                                                        forKey: @"ratingsMessage_shown_on_date" ];

         NSString *lastRatingDateString = [ [NSUserDefaults standardUserDefaults]
                                                objectForKey: @"ratingsMessage_shown_on_date"];
         DebugLog(@"new date: %@", lastRatingDateString);

     }

         [dateFormatter release];
}

谢谢。

4

3 回答 3

13

在进行必要的更改后添加以下代码解决了该问题:

[ [NSUserDefaults standardUserDefaults] synchronize];
于 2009-11-12T22:59:37.297 回答
1

你可以用 Property Editor 查看你的默认值,看看它是否写得正确:

open ~/Library/Preferences/org.your.app.plist

或者只是转储它们:

defaults read org.your.app
于 2009-11-12T23:01:50.663 回答
1

NSuserDefualts 没有在游戏崩溃时保存我的值。通过使用,
[ [NSUserDefaults standardUserDefaults] synchronize]; 淡水河谷得到了拯救。

于 2010-05-21T06:01:26.523 回答