在NSUserDefaults中保存日期(作为NSString)不起作用

|| 我正在尝试将此日期保存在NSUSerDefaults中:
2011-04-14T13:18:25+0000
但是我不想要最后一位(+0000),所以我使用这种方法将其切碎:
NSString *since = [last objectForKey:@\"updated_time\"];
NSString *cutOff = @\"+0000\"; 
NSRange range = [since rangeOfString:cutOff];
self.lastUpdated = [since substringToIndex:range.location];
[[NSUserDefaults standardUserDefaults] setObject:lastUpdated forKey:@\"lastUpdated\"];
如您所见,这只是一个非常简单的操作,它将上面的字符串切成这样:
2011-04-14T13:18:25
但是,当我从NSUserDefaults加载它时,我得到了:
2011-04-14 13:18:25 +0000
谁能告诉我为什么? 更新: 我在.h中像往常一样声明了lastUpdated:
@interface TwitModel
{
    NSString *lastUpdated;
}

@property (retain) NSString *lastUpdated;
在.m中:
@synthesize lastUpdated;
当我要加载它时:
    if(self.lastUpdated == nil) {
        self.lastUpdated = [[NSUserDefaults standardUserDefaults] objectForKey:@\"lastUpdated\"];
    }
    
已邀请:
您需要在
NSUserDefaults
上调用
synchronize
才能正确保存数据:
[[NSUserDefaults standardUserDefaults] synchronize];
编辑 这是我的工作代码:
// Output previous launch\'s stored value
NSLog(@\"Value From Previous Launch: %@\", [[NSUserDefaults standardUserDefaults] valueForKey:@\"lastUpdated\"]);

NSString *since = @\"2011-04-14T13:18:25+0000\";
NSLog(@\"Since: %@\", since);
NSString *cutOff = @\"+0000\"; 
NSRange range = [since rangeOfString:cutOff];
NSString* lastUpdated = [since substringToIndex:range.location];
[[NSUserDefaults standardUserDefaults] setObject:lastUpdated forKey:@\"lastUpdated\"];
[[NSUserDefaults standardUserDefaults] synchronize];

NSLog(@\"Value: %@\", [[NSUserDefaults standardUserDefaults] valueForKey:@\"lastUpdated\"]);
并输出:
2011-04-14 08:52:29.972 TestApp[2117:207] Value From Previous Launch: 2011-04-14T13:18:25
2011-04-14 08:52:29.973 TestApp[2117:207] Since: 2011-04-14T13:18:25+0000
2011-04-14 08:52:29.975 TestApp[2117:207] Value: 2011-04-14T13:18:25
    
很难说-您可以向我们展示重新加载值的代码吗?如果使用日期格式程序以所需格式生成字符串,则至少在概念上可能更简单。或者,仅保存日期本身,并在需要显示日期时担心对其进行格式化。     

要回复问题请先登录注册