0

I have an array with some integers in it, I need to sum them up and then compare. My code works perfectly in swift 2.

var sumedArr:[AnyObject] = textfieldarray.reduce(0, combine: {$0 + $1})
print(sumedArr)

I'm getting this error now.

"Type of expression is ambiguous without more context"

  • so... what is `textfieldarray`? – holex Mar 01 '18 at 09:40
  • Your code works for me when using an array of ints to represent `textfieldarray`. – Jacob King Mar 01 '18 at 09:42
  • I just tried it in a playground with `let textfieldarray = [5,6,7,8]` and all I had to do was remove `combine:` from the reduce call so I ended up with `var sumedArr = textfieldarray.reduce(0, {$0 + $1})` – Hodson Mar 01 '18 at 09:43
  • 1
    or simply `reduce(0, +)` method declaration `func reduce(_ initialResult: Result, _ nextPartialResult: (Result, Int) throws -> Result) rethrows -> Result`. https://stackoverflow.com/questions/28288148/making-my-function-calculate-average-of-array-swift/28288619?s=1|43.9745#28288619 – Leo Dabus Mar 01 '18 at 09:44

0 Answers0