xcode - Type variable in protocol - Swift 2 -
so have protocol, , in want variable class type. way can init class variable.
keep in mind there many different classes. made quick example.
i error "type 'cashregister' not conform protocol 'registerprotocol'"
this example isn't i'm doing, gets point across. 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 bills have in common } class onedollarbill: dollarbillprotocol { required init(){ } } class fivedollarbill: dollarbillprotocol { required init(){ } }
the way declare currentbill
in cashregister
makes var of type class
. protocol registerprotocol
requires variable of type dollarbillprotocol
in class implements protocol. compile error because of mismatch.
to make more clear, declare var explicit type, follows:
class cashregister: registerprotocol { var currentbill: dollarbillprotocol = onedollarbill() // or other initial value }
Comments
Post a Comment