我不理解文档中的这个例子:timeIntervalSinceNow方法应该显示一个正值,但是我们如何才能达到代码中提到的"5“呢?(我认为它要么多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;谢谢你的帮忙
发布于 2012-04-03 11:03:55
如果timeIntervalSinceNow调用的结果是负的(意味着时间戳是过去的(在本例中,它将始终是过去的)),它将被转换为正数。例如,-2.5将变成+2.5 (反之亦然)。
然后测试反转符号值,看看它是否大于5.0 --在本例中,这意味着时间戳来自5秒之前。如果是,则不需要对位置数据做任何操作,因为它太旧而没有用处。
就我个人而言,我会在没有符号倒置的情况下写下这段代码,在测试中使用负数:
if( [[newLocation timestamp] timeIntervalSinceNow] < -5.0 ) return;发布于 2012-04-03 10:27:19
这就是NSDate文档对timeIntervalSinceNow的描述:
接收器与当前日期和时间之间的间隔。如果接收方早于当前日期和时间,则返回值为负。
在这种情况下,时间戳是在过去记录的,并且它将始终早于“现在”。另一方面,如果您使用timeIntervalSince1970,结果将是肯定的。
https://stackoverflow.com/questions/9986168
复制相似问题