2

So I am following this course called "Code With Chris - 14 Day Beginner Challenge (SwiftUI)" (yes I am a beginner), and after each lesson, there is a challenge, I have almost completed the challenge but I couldn't figure out why it wouldn't work, so I checked the dropbox of the completed challenge and I had everything pretty much the same, I have found a solution similar to the source but I still don't understand why my first version (first picture) won't work. I copied everything identically from the source code and it won't work. Is there a possibility that it is the creators of the source code fault, instead of mine?

My expected result is for the "Int" to work just like the "Double" did, The number of people is 5 so I don't see why it wouldn't.

My actual result is an error.

My goal is to complete this challenge:


We’re going to be trying out some math operations in a Swift Playground. Open Xcode and create a new playground (File Menu->New->Playground). From the list of Playground templates, just select “Blank”

Challenge 1 Declare a struct called TaxCalculator Declare a property inside called tax and set it to a decimal value representing the amount of sales tax where you live Declare a method inside called totalWithTax that accepts a Double as an input parameter and returns a Double value. Inside that method, write the code to return a Double value representing the input number with tax included

Challenge 2 Declare a struct called BillSplitter Declare a method inside called splitBy that: has an input parameter of type Double representing a subtotal has an input parameter of type Int representing the number of people returns a Double value Inside that method, use an instance of TaxCalculator (from challenge 1 above) to calculate the total with tax and then split the bill by the number of people passed into the method. Return the amount that each person has to pay.

Challenge 3 Create an instance of BillSplitter Use the instance to print out the amount that each person pays (Assuming 5 people with a bill of $120)


The Code of the course I am using: https://www.dropbox.com/sh/7aopencivoiegz4/AADbxSj83wt6mPNNgYcARFAsa/Lesson%2009?dl=0&file_subpath=%2FL9+Challenge+Solution.playground%2FContents.swift&preview=L9+Challenge+Solution.zip&subfolder_nav_tracking=1

an image of the code with an error

an image of the code without an error

//https://learn.codewithchris.com/courses/take/start/texts/18867185-lesson-9-challenge
//Challenge1
struct TaxCalculator{
    
    var tax = 0.15
    
    func totalWithTax(_ subtotal:Double) -> Double{
        return subtotal * (1 + tax)
    }
}

//Challenge2
struct BillSplitter {
    
    func splitBy(subtotal:Double, numPeople:Int //here is the problem) ->Double {
        
        let taxCalc = TaxCalculator()
        let totalWithTax = taxCalc.totalWithTax(subtotal)
        
        return totalWithTax/numPeople
    }
}

let Split = BillSplitter()
print(Split.splitBy(subtotal: 120, numPeople: 5))
Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80

2 Answers2

2

totalWithTax is a Double. numPeople is an Int.

You need to convert numPeople to a Double too.

return totalWithTax / Double(numPeople)

Operators like / don't work with mismatching types.

aheze
  • 6,076
  • 3
  • 6
  • 40
2

Swift is a bit of a pain with scalar types. Most C family languages will quietly "promote" scalar types to other types as long as there is no loss of data.

byte->int->long int->float->double all happen silently.

In C, this code just works:

int a = 2;
double b = 2.5;

double c = a * b;

The value a gets promoted to a double, and the result is that contains the double value 5.0.

Not so with Swift.

In Swift, you have to explicitly cast a to a double. It won't let you multiply an Int and a Double unless you explicitly cast the Int to a Double, as aheze said in their answer:

return totalWithTax / Double(numPeople)
Duncan C
  • 115,063
  • 19
  • 151
  • 241
  • I know why It doesn't work, but what I want to know is if the course I am using is using a working code. – GrainsGames May 08 '21 at 05:34
  • 2
    The "L9 Challenge Solution" playground fails to run with the error message "Cannot convert value of type 'Int' to expected argument type 'Double'" on line 17. It looks like it has the error. – Duncan C May 08 '21 at 10:38