我从iTunesConnect获得了一堆崩溃日志,用于我的ios swift应用程序,堆栈跟踪顶部显示错误消息:
protocol witness for Strideable.distance(to : A) -> A.Stride in conformance Int64 + 124
这来自我的代码中的无关紧要的行,如下所示:
if (var1 - var2 > MyClass.THRESHOLD) {
// Do something
}
var1和var2声明为Int64类型,而THRESHOLD是:
static let THRESHOLD = 900 * 1000
我有预感,这是因为THRESHOLD没有被宣布为Int64,尽管我仍然没有假设这可能导致问题.此外,该错误是不可重现的,所以我无法验证.
有关此错误消息的含义的任何帮助,以及可能存在的问题?
最佳答案 混合型比较可以
问题的原因.从中推断出减法运算符
其操作数的类型为
@available(swift, deprecated: 3.0, obsoleted: 4.0, message: "Mixed-type subtraction is deprecated. Please use explicit type conversion.")
public func -<T>(lhs: T, rhs: T) -> T.Stride where T : SignedInteger
用T == Int64和T.Stride == Int.您的代码将导致Xcode 8.3.2的警告消息:
let THRESHOLD = 900 * 1000
let var1: Int64 = 0x100000000
let var2: Int64 = 0
// warning: '-' is deprecated: Mixed-type subtraction is deprecated. Please use explicit type conversion.
if var1 - var2 > THRESHOLD {
print("foo")
} else {
print("bar")
}
在32位设备上,Int的差异可能太大
并且上面的示例将因运行时错误而中止
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) frame #0: 0x0223d428 libswiftCore.dylib`protocol witness for Swift.Strideable.distance (to : A) -> A.Stride in conformance Swift.Int64 : Swift.Strideable in Swift + 72
解决方案是明确转换右侧:
if var1 - var2 > Int64(THRESHOLD) { ... }