0

I've created a struct called 'Code', which contains a string and generic property. So, now I'm trying to create a method, which is going to work with the array of Code elements. But the problem is that I'm not aware about the protocol that T-elements should conform to perform + operator.

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
    }

}

Thanks for your help!

Kirill Korolev
  • 966
  • 3
  • 9
  • 22

1 Answers1

0

Arithmetic is the protocol you are after. See https://developer.apple.com/reference/swift/arithmetic

Gary Makin
  • 3,109
  • 1
  • 19
  • 27