i have protocol:
protocol testprotocol { func dosomething }
i use protocol ensure properties conforming like:
class myclass { var detailvc : uiviewcontroller <testprotocol> }
like old objc ensure detailvc conforms testprotocol
protocol myviewcontrollerprotocol { func protofunc() } class myclass { var prop: myviewcontrollerprotocol? }
it's simple that. if want pre-defined class conform protocol, need make extension (but applies class whole) or subclass it.
so...
as extension class whole:
extension uiviewcontroller: myprotocol { func protofunc() { print("do whatever") } }
in case, when extended, can set property as:
var myproperty: uiviewcontroller?
as after being extended, it'll conform required.
or subclass with:
class myconformingviewcontroller: uiviewcontroller, myprotocol { override func protofunc() { print("do whatever") } }
in case, set property as:
var myprop: myconformingviewcontroller?
and that'll automatically confirm myprotocol
due class being set conform it.
you can't force predesignated class conform protocol wasn't designated conform in first place.
e.g. uiviewcontroller wasn't set confirm myotherprotocol
example
that defeat object of protocols in first place. why either extend conform, or subclass conform.
Comments
Post a Comment