calculating 'timeIntervalSinceNow' with a negative value (CLLocation example)?

做~自己de王妃 提交于 2019-12-05 17:41:46

If the result of the timeIntervalSinceNow call is negative (meaning that the timestamp is in the past (which, in this case, it always will be)) it will be converted to a positive number. -2.5 would become +2.5, for example (and vice versa).

Then you test the inverted-sign value, to see if it is greater than 5.0 -- in this case, that means the timestamp is from more than five seconds ago. If it is, you do nothing with the location data, because it's too old to be useful.

Personally, I would have written this without the sign inversion, using a negative number in the test:

 if( [[newLocation timestamp] timeIntervalSinceNow] < -5.0 ) return;

That is what NSDate documentation have to say about timeIntervalSinceNow:

The interval between the receiver and the current date and time. If the receiver is earlier than the current date and time, the return value is negative.

In this case timestamp was recorded in the past, and it will always be earlier than 'now'. If, on the other hand, you would use timeIntervalSince1970, the result would be positive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!