Search code examples
swiftdouble

Cannot convert value of type 'Int' to expected argument type 'Double'


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))

Solution

  • 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.