开发者

Objective-C invalid variables values as a double

I have the following piece of code...

double time = [[NSDate date] timeIntervalSince1970];
double开发者_如何学C startTime = time;
double endTime = time;

When I run the code, the startTime and endTime are not even close to each other. For example, the startTime is 4378480 and the endTime is 1883506224.

Why is there such a difference between the 2 time values?


First, use NSTimeInterval, not double, for all the variable types.

Next -- show the code you are using to print the values. I suspect you are using the wrong format for the variable type (as time intervals always have a decimal component) and are effectively printing garbage as a result.


Can you link the code you are using to output the values, remember you need to use %f not %d in the formatter. The above should be equivalent as both are pointing to the same value


Never mind, the problem I was having was using the incorrect NSLog character.

[NSString stringWithFormat:@"%d", startTime]

When it was supposed to be this.

[NSString stringWithFormat:@"%f", startTime]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜