suse
suse

Reputation: 10563

how to encode/decode of type NSTimeInterval?

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.

Upvotes: 1

Views: 2934

Answers (3)

Johan Kool
Johan Kool

Reputation: 15927

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

Upvotes: 0

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3830

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.

Upvotes: 0

kovpas
kovpas

Reputation: 9593

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"];

Upvotes: 3

Related Questions