extension Array { func sum() -> Int { var sum = 0 for num in self { sum += num } return sum } } [1,2,3].sum()
This code shows what I would like to do. Although I get an error on this line: sum += num . The error I get is: Could not find an overload for '+=' that accepts the supplied arguments .
I suppose the error has something to do with the fact that Array can contain many different types, not just Int, so it turns off. But how to fix it?
source share