我正在尝试使用 EVCloudKitDao 在我的对象上实现 Hashable 协议(protocol)其中所有属性都将通过反射用于散列。对于某些属性,我遇到了错误:
EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT
当我过滤掉有这个问题的 key 时,有时之前正常的 key 会突然出现这个错误。
错误截图如下:
可以在以下位置找到存在此问题的完整代码: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
现在甚至简化并使用 31 * old hash + newhash :
public class func hashValue(theObject: NSObject) -> Int {
return Int(map(toDictionary(theObject)) {$1}.reduce(0) {(31 &* $0) &+ $1.hash})
}
关于ios - .hash 给出 EXC_BREAKPOINT (code=EXC_ARM_BREAKBOINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28503610/