gpt4 book ai didi

objective-c - 用负值计算 'timeIntervalSinceNow'(CLLocation 示例)?

转载 作者:太空狗 更新时间:2023-10-30 03:57:54 24 4
gpt4 key购买 nike

我不明白文档中的这个例子:timeIntervalSinceNow 方法应该显示正值,但我们如何才能达到代码中提到的“5”? (我认为它要么多于 0 要么少于 0,或者 -10 、-20、-30 等...但是我们怎样才能得到一个正值,比如 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(反之亦然)。

然后您测试倒符号值,看它是否大于 5.0——在这种情况下,这意味着时间戳来自五秒前。如果是,则您无需对位置数据执行任何操作,因为它太旧而无用。

就个人而言,我会在没有符号反转的情况下编写这个,在测试中使用负数:

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

关于objective-c - 用负值计算 'timeIntervalSinceNow'(CLLocation 示例)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com