Variable type in the protocol - Swift 2

So, I have a protocol, and in it I want a class type variable. That way I can initialize this class from a variable.

Keep in mind that there will be many different classes. I made a quick example.

I get the error "type 'CashRegister' does not match the protocol 'RegisterProtocol'"

This example is not exactly what I am doing, but it makes sense. Thanks for the help.

protocol RegisterProtocol {
    var currentBill: DollarBillProtocol {get set}
    func makeNewBill()->DollarBillProtocol
}

extension RegisterProtocol {
    func printCurrentBill() {
        Swift.print(currentBill)
    }
}

class CashRegister: RegisterProtocol {

    var currentBill = OneDollarBill.self

    func makeNewBill() -> DollarBillProtocol {
        return currentBill.init()
    }
}



protocol DollarBillProtocol {
    // protocol that all bills have in common
}


class OneDollarBill: DollarBillProtocol {
    required init(){
    }
}

class FiveDollarBill: DollarBillProtocol {
    required init(){
    }

}
+1
source share
1 answer

currentBill CashRegister var class. RegisterProtocol , DollarBillProtocol , . - .

, var , :

class CashRegister: RegisterProtocol {

    var currentBill: DollarBillProtocol = OneDollarBill() // or other initial value
}
0

All Articles