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

Yau*_*nka 3 xcode objective-c

在下一个obj-c代码中有问题错误.

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

xCode发送错误:

error: Semantic Issue: Arithmetic on pointer to interface 'NSDate', which is not a constant size in non-fragile ABI
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

jus*_*tin 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 */
}
Run Code Online (Sandbox Code Playgroud)

具体而言,timestamp是类型的属性NSDate,而不是标量数字,如NSTimeInterval.