指向接口'NSDate'的指针的算法,它在非脆弱的ABI中不是常量

时间:2011-09-05 10:38:08

标签: objective-c xcode

在下一个obj-c代码中出现问题。

if (fabs(originalLocation.timestamp - ((CLLocation *)[lastLocations objectAtIndex:i]).timestamp) > constAverageLocationTimeout) 
{ 
    //do 
}

xCode发送错误:

error: Semantic Issue: Arithmetic on pointer to interface 'NSDate', which is not a constant size in non-fragile ABI

有任何想法吗?

1 个答案:

答案 0 :(得分:6)

你的程序(以及编译器错误的位置)如果你把它分解一下会更有意义。也许是这样的:

NSDate * orginalDate = originalLocation.timestamp;
CLLocation * lastLocation = [lastLocations objectAtIndex:i];
NSDate * lastDate = lastLocation.timestamp;

NSTimeInterval originalTime = [originalDate timeIntervalSinceReferenceDate];
NSTimeInterval lastTime = [lastDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsed = fabs(originalTime - lastTime);

if (elapsed > constAverageLocationTimeout) {
  /* do */
}

具体而言,timestampNSDate类型的属性,而不是标量数字,例如NSTimeInterval。