NSNumber and NSTimeInterval
I am trying to get the system time in milliseconds. For that I have declared:
NSNumber *createdTimeInMilli开发者_运维问答Sec; //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
? Or what I am doing wrong?
NSTimeInterval
is typedefed as follow : typedef double NSTimeInterval;
.
To create a NSNumber
with, use :
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. You can get an NSNumber from it by doing [NSNumber numberWithDouble:timeInterval]
or even more succinctly @(timeInterval)
.
NSTimeInterval is a typedef for a double. So use NSNumber
's convenience constructor numberWithDouble:
as follows:
self.createdTimeInMilliSec= [NSNumber numberWithDouble:([NSDate timeIntervalSinceReferenceDate]*1000)];
As NSTimeInterval is a double, you could do
NSNumber *myNumber = [NSNumber numberWithDouble: myTimeInterval];
精彩评论