I’m trying to show a UIALertView every 4 days. But getting a crash because:
NSInvalidArgumentException', reason: '-[__NSCFString timeIntervalSince1970]:
I tried using different approaches like the following code here: https://stackoverflow.com/a/4278151/1014564 resulting in the same crash.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *firstLaunchDate = [defaults objectForKey:@"timeStamp"];
NSDate *dateNow = [[NSDate alloc] init];
if (!firstLaunchDate){
NSString *nowTimestamp = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
[defaults setObject:nowTimestamp forKey:@"timeStamp"];
[defaults synchronize];
} else if (([dateNow timeIntervalSince1970] - [firstLaunchDate timeIntervalSince1970]) > 86400*4){
///UIAlertView Here..
}
Answers appreciated and will be accepted. I know it’s probably a simple mistake, I’m past the point of sleepy atm.
then
So you’re basically setting an
NSStringin the user defaults, but you expect to get back anNSDate. SinceNSUserDefaultsain’t magic, that won’t happen. If you want to store a date, store it usingand only then you can use
timeIntervalSince1970on the object you get back fromNSUserDefaults.