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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -