我创建了一个名为“Code”的结构,其中包含一个字符串和通用属性。所以,现在我正在尝试创建一个方法,它将与 Code 元素数组一起使用。但问题是我不知道 T 元素应该遵守执行 + 运算符的协议(protocol)。
struct Code<Element>{
let probability: Element
let code: String
}
func createTree<T>(_ array: [Code<T>]){
for i in 0..<array.count-1{
let sum = array[i].probability + array[i+1].probability
}
}
感谢您的帮助!
最佳答案
Arithmetic
是您所追求的协议(protocol)。请参阅https://developer.apple.com/reference/swift/arithmetic
关于arrays - 我应该遵循哪种协议(protocol)才能在 Swift 中将一个通用值添加到另一个通用值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268379/