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 trialKami Wang
5,643 PointsReturning Complex Values-- I wrote exactly the same code which in the video, but there are three errors. Why?
func carpetCostCalculator(length length: Int, width: Int, carpetColor: String = "tan") -> (price: Int, carpetColor: String) {
// Gray Carpet - 1$/sq ft
// Tan Carpet - 2$/sq ft
// Deep Blue Carpet - 4$/sq ft
let area = calcalateArea(length, width: width)
var price: Int
switch carpetColor {
case "gray":
price = area * 1
case "tan":
price = area * 2
case "blue":
price = area * 4
default:
price = 0
}
return (price, carpetColor)
}
let result = carpetCostCalculator(length: 10, width: 20) [Error:Ambiguous use of 'carpetCostCalculator(length:width:carpetColor:)'
result.price [Error:Value of type 'Int' has no member 'price']
result.carpetColor [Error:Value of type 'Int' has no member 'carpetColor']
1 Answer
Michael Hulet
47,913 PointsSomewhere, you have carpetCostCalculator(length:width:carpetColor:)
defined more than once, and the multiple different implementations are defined to accept and/or return
different types, and Swift doesn't know which one to use. Solve that error by specifying what you want where there is ambiguity, and your code should work (in a Playground)