Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

abdirahman liban
PLUS
abdirahman liban
Courses Plus Student 2,163 Points

Variadic Parameters

I wrote this code in xcode :

func mean (numbers:Double...)-> Double {


    var total : Double = 0

    for number in numbers {

        total += numbers
    }

    return total / Double(numbers.count)

}

but is show me this error : binary operator "+=" cannot be applied to operands of type double and double

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Careful with that error message - I imagine it's actually telling you it can't apply += to operands of type Double and [Double], because you're trying to add the whole numbers array!

You want total += number, not total += numbers. :smile:

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Try doing it the long way to see if it works: total = total + numbers

abdirahman liban
abdirahman liban
Courses Plus Student 2,163 Points

it show me error : cannot convert value of type '[Double]' to expected argument type 'Double'

Greg Kaleka
Greg Kaleka
39,021 Points

Yep - that error is telling you exactly what you need to know. You can't convert an array of Doubles into a Double. That's what [Double] is - an array of Doubles.

abdirahman liban
abdirahman liban
Courses Plus Student 2,163 Points

it show me error : cannot invoke intializer for type 'Double' for type 'Double ' with an argument list of '((Double))'