Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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