Level 9(14 days Challenge)

After running L9 challenge solution, i got this error as below

Error: L9 Challenge Solution.playgound:17:29: error: cannot convert value of type ‘Int’ to expected argument type ‘Double’ return totalWithTax/numPeople

Please help what I need to change

I ran into the same problem. I changed the number of people from an Int to a Double and it worked properly. From what I understand you can’t mix doubles and integers.

My thinking is that it makes sense to declare the number of people as an integer, as there can only be 1 / 2 / 3 / 4 / … people but not 1.2 people or similar. Using an Int will give you correct data.

To do some calculations with this Int number, you need to convert all numbers in this calculation to be the same (or similar?) type, for example doubles. But the conversation is really easy in SwiftUI.

Had the same exact issue and the change from Int to Double worked. The question i have about this challenge is about the underscore before subtotal in the func totalWithTax. Causes an error with the instance taxCalc.totalWithTax(subtotal) if it’s not there. Curious as to why it needs to be there. Any explanations?

This explanation may help you

let tax = 0.15  // 15%

//  The itemPrice parameter name appears at the function call site
func totalWithTax(itemPrice: Double) -> Double {
    itemPrice * (1 + tax)
}
print(totalWithTax(itemPrice: 120))


//  The underscore causes the itemPrice parameter to not appear
//  at the function call site
func totalWithTax(_ itemPrice: Double) -> Double {
    itemPrice * (1 + tax)
}
print(totalWithTax(120))


//  The word 'using' is an argument label and appears at the function call
//  site in place of itemPrice
func totalWithTax(using itemPrice: Double) -> Double {
    itemPrice * (1 + tax)
}
print(totalWithTax(using: 120))

You will note that the function name is exactly the same in each case and you might wonder why the compiler does not say, “Invalid redeclaration of ‘totalWithTax()’” but the reason is that the signature is different in each case.

I hope that helps.

2 Likes

I appreciate the response and explanation. Now that I think about it, I do remember Chris mentioning about the use of argument labels and the underscore.

What about the data type for numPeople? The compiler gave me errors when the data type was set to Int. When I changed it to Double the errors went away.

I think the data type was deliberately suggested to be an Int in the parameters for the function splitBy. You could have either wrapped people to cast is as a Double() by doing this on line 18:

return totalWithTax / Double(people)

or do what you did by changing the parameter type to Double.

Either way, it reinforces that any calculations performed, must be with the same data types. It’s something that you wont forget.

2 Likes

Ok makes sense, thanks for your help.

I would add to that explanation, that for ‚people‘ Int makes more sense than Double: You can only have 1, 2, 3, … people, but not 1.5 or 3.7 people. Only allowing Int values for ‚people‘ makes the code more robust against wrong entries.

Yeah but logic tells you people are in whole units rather than decimals so nobody is going to divide up a bill by 2.5 people.