XMarshall
XMarshall

Reputation: 971

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? Or what I am doing wrong?

Upvotes: 6

Views: 20150

Answers (4)

tophyr
tophyr

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

dasdom
dasdom

Reputation: 14063

As NSTimeInterval is a double, you could do

NSNumber *myNumber = [NSNumber numberWithDouble: myTimeInterval];

Upvotes: 1

user756245
user756245

Reputation:

NSTimeInterval is typedefed as follow : typedef double NSTimeInterval;.

To create a NSNumber with, use :

NSNumber *n = [NSNumber numberWithDouble:yourTimeIntervalValue];

Upvotes: 26

albertamg
albertamg

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

Related Questions