Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,018 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,485 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,018 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))'