views:

241

answers:

3

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

+2  A: 

Simple. You need to cast it to an NSTimeInterval.

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

timeInterval = (NSTimeInterval) tempDouble;
Jacob Relkin
Knew there was a simple answer, thanks a lot
culov
+1  A: 

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.

Jon Hess
+1  A: 

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.

dreamlax