Pau*_*aul 4 objective-c nstimeinterval cllocation
我不明白这个例子来自doc:timeIntervalSinceNow方法应该显示一个正值,但是我们怎么能达到代码中提到的"5"?(我认为它或者更多或更少,或者-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;
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
如果timeIntervalSinceNow调用的结果是否定的(意味着时间戳是过去的(在这种情况下,它总是如此))它将被转换为正数.例如,-2.5会变成+2.5(反之亦然).
然后测试反转符号值,看它是否大于5.0 - 在这种情况下,这意味着时间戳超过五秒前.如果是,则不对位置数据执行任何操作,因为它太旧而无法使用.
就个人而言,我会在没有符号反转的情况下编写这个,在测试中使用负数:
if( [[newLocation timestamp] timeIntervalSinceNow] < -5.0 ) return;
Run Code Online (Sandbox Code Playgroud)