简体   繁体   中英

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.

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 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.

NSTimeInterval是双NSTimeInterval ,因此可以使用-(void)encodeDouble:(double)realv forKey:(NSString *)key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM