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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -