D-Nice
D-Nice

Reputation: 4870

Convert a string of numbers to a NSTimeInterval

I know I must be over-complicating this because it NSTimeInterval is just a double, but I just can't seem to get this done properly since I have had very little exposure to objective c. the scenario is as follows:

The data im pulling into the app contains two values, startTime and endTime, which are the epoch times in milliseconds. The variables that I want to hold these values are NSTimeInterval *start; NSTimeInterval *end;

I decided to store them as NSTimeIntervals but im thinking that maybe i ought to store them as doubles because theres no need for NSTimeIntervals since comparisons can just be done with a primitive. Either way, I'd like to know what I'm missing in the following step, where I try to convert from string to NSTimeInterval:

    tempString = [truckArray objectAtIndex:2];
    tempDouble = [tempString doubleValue];

Now it's safely stored as a double, but I can't get the value into an NSTimeInterval. How should this be accomplished? Thanks

Upvotes: 2

Views: 11962

Answers (2)

dreamlax
dreamlax

Reputation: 95335

The variables that I want to hold these values are NSTimeInterval *start; NSTimeInterval *end;

Careful, NSTimeInverval is a typedef for a primitive C type, it is not an Objective-C object. I don't think you actually need pointers to these types in this scenario, so you should declare them like this:

NSTimeInverval start;
NSTimeInterval end;

You could be getting errors because in C, you cannot convert floating-point types to pointer-types.

Upvotes: 2

Jon Hess
Jon Hess

Reputation: 14247

You don't have to cast, you can just write this:

NSTimeInterval timeInterval = [[truckArray objectAtIndex:2] doubleValue];

The cast is needless, and extra casts just make your source code harder to update and change in the future because you've told the compiler not to type-check your casted expressions.

Upvotes: 12

Related Questions