Reputation: 971
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
? Or what I am doing wrong?
Upvotes: 6
Views: 20150
Reputation: 1678
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)
.
Upvotes: 2
Reputation: 14063
As NSTimeInterval is a double, you could do
NSNumber *myNumber = [NSNumber numberWithDouble: myTimeInterval];
Upvotes: 1
Reputation:
NSTimeInterval
is typedefed as follow : typedef double NSTimeInterval;
.
To create a NSNumber
with, use :
NSNumber *n = [NSNumber numberWithDouble:yourTimeIntervalValue];
Upvotes: 26
Reputation: 28572
NSTimeInterval is a typedef for a double. So use NSNumber
's convenience constructor numberWithDouble:
as follows:
self.createdTimeInMilliSec= [NSNumber numberWithDouble:([NSDate timeIntervalSinceReferenceDate]*1000)];
Upvotes: 1