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 trialabdirahman liban
Courses Plus Student 2,163 PointsVariadic 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
39,021 PointsCareful 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
.
Cindy Lea
Courses Plus Student 6,497 PointsTry doing it the long way to see if it works: total = total + numbers
abdirahman liban
Courses Plus Student 2,163 Pointsit show me error : cannot convert value of type '[Double]' to expected argument type 'Double'
Greg Kaleka
39,021 PointsYep - 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.
Cindy Lea
Courses Plus Student 6,497 PointsTry this:
total += Double(numbers)
abdirahman liban
Courses Plus Student 2,163 Pointsit show me error : cannot invoke intializer for type 'Double' for type 'Double ' with an argument list of '((Double))'
abdirahman liban
Courses Plus Student 2,163 Pointsabdirahman liban
Courses Plus Student 2,163 Pointsthank you