Вопрос

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.

Это было полезно?

Решение

nowTimestamp = [NSString stringWithFormat:...];
[defaults setObject:nowTimestamp forKey:@"timeStamp"];

then

NSDate *firstLaunchDate = [defaults objectForKey:@"timeStamp"];

So you're basically setting an NSString in the user defaults, but you expect to get back an NSDate. Since NSUserDefaults ain't magic, that won't happen. If you want to store a date, store it using

[defaults setObject:[NSDate date] forKey:@"timeStamp"];

and only then you can use timeIntervalSince1970 on the object you get back from NSUserDefaults.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top