swift - Swift 类型错误 :Cannot convert value of type '[UInt64]' to expected argument type 'inout UInt64'

标签 swift swift4

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/

相关文章:

ios - 将阴影和角半径应用于 View ,而不是整个单元格

ios - 比较相同的日期返回 OrderedAscending

ios - 无法将类型 'Dictionary<String?, String?>.Keys' 的值分配给类型 'String'

ios - 如何使用 Swift 包管理器添加依赖项

ios - 使用 Core Data 保存图像

swift 4 在 UIRefeshControl 之前清空 UITableView

swift - 了解 Vapor-Fluent 中的迁移(服务器端 Swift)

ios - 在 swift 模块(框架)中使用依赖

swift - swift segue不起作用(初学者)

ios - 我无法在后台播放音乐(Xcode 9、Swift 4)