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 trial

iOS Build a Vending Machine App in Swift 2.0 Loading Data From a Resource Type Casting

I don't see the download for the new Playground only Code Snippets

I'm on Type casting in the Building a Vending Machine App in Swift lesson and I can't seem to see the download for the entirety of the code discussed just a small snippet that doesnt include everything

Stijn van Lieshout
Stijn van Lieshout
3,646 Points

Yes, the employees array is missing from the code snippet. I typed it out using different placeholder values below.

let employees = [ 
    HourlyEmployee(name: "First", hourlyWage: 12.00, hoursWorked: 100.00),
    SalariedEmployee(name: "Second", salary: 12.00),
    SalariedEmployee(name: "Third", salary: 12.00),
    HourlyEmployee(name: "Fourth", hourlyWage: 12.00, hoursWorked: 100.00),
    SalariedEmployee(name: "Fifth", salary: 12.00),
    HourlyEmployee(name: "Sixth", hourlyWage: 12.00, hoursWorked: 100.00)
]
Clark Tappy
Clark Tappy
2,847 Points

Why don't you just add it to the main page of code snippets below the related video so we don't have to go searching for the missing pieces?

1 Answer

This expands on Stijn's work, In case you want all the code:

class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }
}

class HourlyEmployee: Employee {
    let hourlyWage: Double
    let hoursWorked: Double

    init(name: String, hourlyWage wage: Double, hoursWorked hours: Double) {
        self.hourlyWage = wage
        self.hoursWorked = hours
        super.init(name: name)
    }

    func payWages() -> Double {
        return hourlyWage * hoursWorked
    }
}

class SalariedEmployee: Employee {
    let salary: Double
    init(name: String, salary: Double) {
        self.salary = salary
        super.init(name: name)
    }

    func paySalary() -> Double {
        return salary
    }
}

let employees = [
    HourlyEmployee(name: "First", hourlyWage: 12.00, hoursWorked: 100.00),
    SalariedEmployee(name: "Second", salary: 12.00),
    SalariedEmployee(name: "Third", salary: 12.00),
    HourlyEmployee(name: "Fourth", hourlyWage: 12.00, hoursWorked: 100.00),
    SalariedEmployee(name: "Fifth", salary: 12.00),
    HourlyEmployee(name: "Sixth", hourlyWage: 12.00, hoursWorked: 100.00)
]

for employee in employees {
    if employee is HourlyEmployee {
        let hourlyEmployee = employee as! HourlyEmployee
        hourlyEmployee.payWages()
    }

    if employee is SalariedEmployee {
        if let salariedEmployee = employee as? SalariedEmployee {
            salariedEmployee.paySalary()
        }
    }
}