简体   繁体   English

NSNumber和NSTimeInterval

[英]NSNumber and NSTimeInterval

I am trying to get the system time in milliseconds. 我试图以毫秒为单位获得系统时间。 For that I have declared: 为此,我宣布:

 NSNumber *createdTimeInMilliSec; //in class declaration

and in one of my instance functions, I doing: 在我的一个实例函数中,我在做:

 self.createdTimeInMilliSec= ([NSDate timeIntervalSinceReferenceDate]*1000); //ERROR: incompatible type for argument 1 of 'setCreatedTimeInMilliSec:'

timeIntervalSinceReferenceDate returns in NSTimeInterval , so how to convert that into NSNumber ? timeIntervalSinceReferenceDateNSTimeInterval返回,那么如何将其转换为NSNumber Or what I am doing wrong? 或者我做错了什么?

NSTimeInterval is typedefed as follow : typedef double NSTimeInterval; NSTimeInterval的类型定义如下: typedef double NSTimeInterval; .

To create a NSNumber with, use : 要创建NSNumber ,请使用:

NSNumber *n = [NSNumber numberWithDouble:yourTimeIntervalValue];

I'm not sure it's clear from the other answers - but NSTimeInterval is actually just a typedef'ed double. 我不确定其他答案是否清楚 - 但NSTimeInterval实际上只是一个typedef'ed double。 You can get an NSNumber from it by doing [NSNumber numberWithDouble:timeInterval] or even more succinctly @(timeInterval) . 您可以通过[NSNumber numberWithDouble:timeInterval]或甚至更简洁的@(timeInterval)来获取NSNumber。

NSTimeInterval is a typedef for a double. NSTimeInterval是double的typedef。 So use NSNumber 's convenience constructor numberWithDouble: as follows: 所以使用NSNumber的便利构造函数numberWithDouble:如下:

self.createdTimeInMilliSec= [NSNumber numberWithDouble:([NSDate timeIntervalSinceReferenceDate]*1000)];

由于NSTimeInterval是双倍的,你可以做到

NSNumber *myNumber = [NSNumber numberWithDouble: myTimeInterval];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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