NSNumber and NSTimeInterval

I am trying to get the system time in milliseconds. For this, I stated:

NSNumber *createdTimeInMilliSec; //in class declaration 

and in one of my instance functions, I do:

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

timeIntervalSinceReferenceDate returns to NSTimeInterval , so how do I convert it to NSNumber ? Or what am I doing wrong?

+6
source share
4 answers

NSTimeInterval is printed as follows: typedef double NSTimeInterval; .

To create an NSNumber with, use:

 NSNumber *n = [NSNumber numberWithDouble:yourTimeIntervalValue]; 
+22
source

I'm not sure if this is clear from the other answers, but NSTimeInterval is actually just a double type. You can get an NSNumber from it by running [NSNumber numberWithDouble:timeInterval] or even more concisely @(timeInterval) .

+2
source

NSTimeInterval is a typedef for double. Therefore, use the convenience constructor NSNumber numberWithDouble: as follows:

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

Since NSTimeInterval is double, you can do

 NSNumber *myNumber = [NSNumber numberWithDouble: myTimeInterval]; 
+1
source

Source: https://habr.com/ru/post/893496/


All Articles