Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift error cannot invoke '+' with an argument list of type '($T28, $T32)'

Tags:

swift

I'm getting a strange error that I can't help but think is a compiler error. In Swift, in either a Playground or in an iOS app, if I do the following...

let array = [0, 1, 2, 3, 4, 5]
let sum = array[0] + array[1] + array[2] + array[3] + array[4] + array[5]

...I get the following compiler error.

cannot invoke '+' with an argument list of type '($T28, $T32)'

Just for grins, I changed that to an array of strings and I get the same. However, if I just add the first five values, no error. Leaving aside the fact that this is not the best way to sum up these numbers, how is this not a compiler error?

And if it isn't a compiler error, why isn't it?

like image 612
Mallioch Avatar asked Jan 31 '26 06:01

Mallioch


1 Answers

if you try to create an Integer Array:

let array:[Int] = [0, 1, 2, 3, 4, 5] 

I had the same issue. I guess to compiler did not recognize the type of your array values.

If you use:

let sum = Int(array[0]) + Int(array[1]) + Int(array[2]) + Int(array[3]) + Int(array[4]) + Int(array[5])

it works as expected.

like image 175
derdida Avatar answered Feb 03 '26 00:02

derdida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!