如何编码/解码 NSTimeInterval 类型?
如何编码和解码 NSTimeInterval?
当我这样做时......
NSTimeInterval refreshInterval;
[coder encodeObject:refreshInterval forKey:@"refreshinterval"];
它会抛出一个错误,说,类型不兼容
如何解决这个问题?
或者相应的事情是将 NSTimeInterval 转换为 int ,反之亦然..但我没有找到任何转换方法..
请建议我如何解决这个问题。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDate.h:
因此,您可以将 NSTimeInterval 视为 double 。转换为 int -
(int)interval
或round(interval)
。重新编码,我不完全确定,但似乎,encodeObject接收(id)作为参数。 double 是一个原子类型,所以你应该用 NSNumber 包装它:
NSDate.h:
So, you may treat to NSTimeInterval as double. Conversion to int -
(int)interval
orround(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:
kovpas 比我先一步:)
是的,变量不是对象,NSNumber 返回一个对象,encodeObject 方法知道如何处理该对象。您可能必须使用 [NSNumber numberWithDouble:(double)refreshInterval],但我真的不这么认为。如果你想跳过小数位,你需要 (int) 和 numberWithInt 来代替。
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
是一个 double,因此您可以使用-(void)encodeDouble:(double)realv forKey:(NSString *)key
。NSTimeInterval
is a double, so you can use-(void)encodeDouble:(double)realv forKey:(NSString *)key
.