.hash 给出 EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT

.hash give EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT

我正在尝试使用 EVCloudKitDao 在我的对象上实现 Hashable 协议,其中所有属性都将通过反射用于散列。对于某些属性,我收到错误:

EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT

当我过滤掉有这个问题的密钥时,有时之前正常的密钥会突然出现这个错误。

错误截图如下:

存在这个问题的完整代码可以在以下位置找到:EVReflection.swift

我在以 EVCloudKitDataObject 作为基础对象的对象上请求 .hash 时收到错误。

此问题是由于 Int 溢出引起的。更改代码后,您在下面看到的错误消失了:

    public class func hashValue(theObject: NSObject) -> Int {
    var hash : Int = 0
    var counter : Int = 0
    for (key, value) in toDictionary(theObject) {
        hash = hash &+ (value.hash << counter)
        counter = counter + 1
    }
    return Int(hash)
}

有关详细信息,请参阅位于 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html

的苹果文档中的 'Overflow operators' 部分

现在甚至简化并使用 31 * old hash + newhash :

public class func hashValue(theObject: NSObject) -> Int {
    return Int(map(toDictionary(theObject)) {}.reduce(0) {(31 &* [=11=]) &+ .hash})
}