Force child classes to implement protocol swift -
how can forced child class implement protocol declared in parent class?
i tried this:
protocol myprotocol { var myvar : string { } } class parentclass: myprotocol { var myvar = "parent" } class childclass: parentclass { }
but child class doesn't force me override myvar.
this possible ?
thank much,
morgan
as far knowledge not possible in swift. if try conforming parent class's protocol, leads error "cannot override stored property". since protocol conformed in parentclass.
protocol myprotocol { var myvar : string { } } class parentclass: myprotocol { var myvar = "parent" } class childclass: parentclass { var myvar = "hello" // throws compilation error, "cannot override stored property" since it's conformed parentclass itself. }
added:
in general words multi-level implementation of interface not possible, in ios words protocol's should implemented @ single level only. since you've inherited parentclass, childclass has scope access parentclass members.
class childclass: parentclass, myprotocol { func printvalue(){ println("newvalue : \(myvar)") myvar = "hello" } }
hope helps...!
Comments
Post a Comment