DaveyDaVinci
DaveyDaVinci

Reputation: 37

Converting an NSNumber to NSTimeInterval

I've converted an NSTimeInterval to an NSNumber in order to store it in NSUserDefaults. I used the numberWithDouble method like this:

NSNumber *savedTime = [NSNumber numberWithDouble:timeInterval];

I'm retrieving the information in another view, but I'm running into an issue. The number doesn't seem to want to convert properly. I know the number is carrying over, because I am able to view the number in this label, and it shows up properly:

timeLabel.text = [NSString stringWithFormat:@"%f", [[[userDefaults objectForKey:groupTitleForReference] objectForKey:@"time"] doubleValue]];

but when I go to set the interval to the double value, it doesn't work. I'm using this:

interval = [[[userDefaults objectForKey:groupTitleForReference] objectForKey:@"time"] doubleValue];

All variables are properly declared in the header. Any idea why it's not working?

Thanks,

David

Upvotes: 1

Views: 2750

Answers (1)

JeremyP
JeremyP

Reputation: 86651

In both cases, you use

[[[userDefaults objectForKey:groupTitleForReference] objectForKey:@"time"] doubleValue]

to get the double value. You need to concentrate on what might be different. Off the top of my head, look at:

  • Is interval correctly declared as a double or NSTimeInterval? Show us how and where you declare interval.
  • When you display interval to check its value, are you displaying it correctly. I've been known to do this by accident

    NSTimeInterval interval = ....
    NSLog(@"%@", interval);
    

    Show us the code you use to inspect the interval and its output.

  • Has userDefaults changed?

  • Has groupTitleForReference changed.

EDIT

Another thing to check

  • If interval is a global make sure the declaration in the header is of the form

    extern NSTimeInterval interval;
    

    If you omit extern, you'll get a separate variable called interval in every file that includes the header.

Upvotes: 1

Related Questions