views:

29

answers:

3

Hello,

How do i encode and decode NSTimeInterval?

when i do it in this way...

NSTimeInterval refreshInterval;

[coder encodeObject:refreshInterval forKey:@"refreshinterval"];

it throws an error saying , incompatible type

How to solve this problem?

or the correspoinding thing is to convert NSTimeInterval to int and vice-verse.. but i'm not finding any method for convertion..

please do suggest me how to solve this problem.

Thank You.

+1  A: 

NSDate.h:

typedef double NSTimeInterval;

So, you may treat to NSTimeInterval as double. Conversion to int - (int)interval or round(interval).

Re coder, I'm not completely sure, but it seems, that encodeObject receives (id) as a parameter. double is an atomic type, so you should wrap it with NSNumber:

[coder encodeObject:[NSNumber numberWithDouble:refreshInterval] forKey:@"refreshinterval"];
kovpas
A: 

kovpas beat me to it :)

Yeah, variables are not objects and NSNumber returns an object that the method encodeObject knows what to do with. You might have to use [NSNumber numberWithDouble:(double)refreshInterval], but I really don't think so. If you want to skip decimal places you need (int), and numberWithInt instead.

Henrik Erlandsson
A: 

NSTimeInterval is a double, so you can use -(void)encodeDouble:(double)realv forKey:(NSString *)key.

Johan Kool