我正在用少量代码将date1与date2进行比较,并收到崩溃通知。
我在NSUserDefaults中获得date1,它是:
NSDate *date1 = [[NSUserDefaults standardUserDefaults]
objectForKey:@"date1"];
NSDate *date2 = [[NSDate alloc] init];
if (fabs([date2 timeIntervalSinceDate:date1]) < 60)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"60 seconds or less" message:@"Text" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
return;
}
我将date1存储在NSUserDefaults中,如下所示:
NSDate *date1 = [[NSDate alloc] init];
[[NSUserDefaults standardUserDefaults] setObject:date1 forKey:@"date1"];
[[NSUserDefaults standardUserDefaults] synchronize];
该应用程序崩溃,原因是-[__ NSCFString timeIntervalSinceReferenceDate]:无法识别的选择器已发送到实例。谁有想法?
谢谢!
最佳答案
我已经编译了您的代码,它对我有用。看起来是这样的:
NSDate *date1test = [[NSDate alloc] init];
[[NSUserDefaults standardUserDefaults] setObject:date1test forKey:@"date1"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSDate *date1 = [[NSUserDefaults standardUserDefaults]
objectForKey:@"date1"];
NSDate *date2 = [[NSDate alloc] init];
if (fabs([date2 timeIntervalSinceDate:date1]) < 60)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"60 seconds or less" message:@"Text" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
return;
}
看看这是否为您编译
关于objective-c - 在NSUserDefaults中存储日期引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13931186/