0

I'm wondering, in objective-c, how can I convert a float type primative number to NSTimeInterval?

The float type value represents the number of seconds in my case

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • see this http://stackoverflow.com/questions/1259028/convert-nsnumber-double-value-into-time – Anbu.Karthik Aug 10 '16 at 07:47
  • Just cast to a `double`. What's the problem? e.g. `NSTimeInterval interval = (NSTimeInterval)myFloatyTime;`. You don't even need the cast. – Avi Aug 10 '16 at 07:49
  • @Avi, thanks, I don't know that, so I ask. Now I know :) – Mellon Aug 10 '16 at 07:56
  • You shouldn't be using float at all unless you have a very good reason, but you should be using double. float has very low precision. Use double to start with. – gnasher729 Aug 10 '16 at 08:06
  • in ObjC: `typedef double NSTimeInterval;` or in Swift: `public typealias NSTimeInterval = Double`... so... how to convert a `float` to `double`...? – holex Aug 10 '16 at 09:03

1 Answers1

3

NSTimeInterval is just typedef to a double, so there is nothing to do in case of a float. No trouble at all... :)

        float t = 5;
        NSTimeInterval ti = t;

Cheers.

Florian Burel
  • 3,408
  • 1
  • 19
  • 20