使用负值计算“timeIntervalSinceNow"(CLLocation 示例)? [英] calculating 'timeIntervalSinceNow' with a negative value (CLLocation example)?

查看:87
本文介绍了使用负值计算“timeIntervalSinceNow"(CLLocation 示例)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白文档中的这个例子:timeIntervalSinceNow 方法应该显示一个正值,但是我们如何达到代码中提到的5"?(我认为它要么多于 0 要么少于 0,或者 -10 、-20、-30 等等……但是我们如何获得正值,例如 5?):

i don't understand this example from the doc : the timeIntervalSinceNow method should show a positive value, but how can we reach "5" as mentioned in the code? (i think it's either 0 more or less, or -10 , -20, -30 etc... but how can we get a positive value such as 5?) :

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

感谢您的帮助

推荐答案

如果 timeIntervalSinceNow 调用的结果是否定的(意味着时间戳是过去的(在这种情况下,它总是会)) 它将被转换为正数.例如,-2.5 会变成 +2.5(反之亦然).

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).

然后您测试反转符号值,看看它是否大于 5.0——在这种情况下,这意味着时间戳来自 5 秒多之前.如果是,您就不用处理位置数据,因为它太旧而无用.

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;

这篇关于使用负值计算“timeIntervalSinceNow"(CLLocation 示例)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆