Swift 新手,我遇到了这个错误:
Cannot convert value of type '[UInt64]' to expected argument type 'inout UInt64'
我不明白“inout”打字
/// non crypto hash
func strHash(_ str: String) -> UInt64 {
var result = UInt64 (5381)
let buf = [UInt8](str.utf8)
for b in buf {
result = 127 * (result & 0x00ffffffffffffff) + UInt64(b)
}
return result
}
let myString: String = "Hello World"
let words = myString.components(separatedBy: " " )
print(words)
var hashArry = [UInt64]()
for w in words {
hashArry += strHash(w) // <<<<<<<<< Here
}
最佳答案
嗯,您不能像您预期的那样使用 +=
。
使用
hashArray.append(strHash(w))
相反。并且不要对有时非常困惑的编译器错误消息感到奇怪:-)
关于swift - Swift 类型错误 :Cannot convert value of type '[UInt64]' to expected argument type 'inout UInt64' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663308/