how to encode/decode of type NSTimeInterval?
-
01-10-2019 - |
문제
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
is a double, so you can use -(void)encodeDouble:(double)realv forKey:(NSString *)key
.