swift - how to declare a generate protocol as delegate? -
we can make generate protocol follow:
protocol somedelegate { typealias t func xxx(x: t) }
and make class conform it:
class aa: somedelegate { typealias t = int func xxx(x: t) { // thing } }
and problem how declare propert conform generate protocol this:
class bb { var delegate: somedelegate }
the code on above raise error:
protocol 'somedelegate' can used generic constraint because has self or associated type requirements
it seems can use protocol delegate follow:
class bb { var delegate: aa? }
but, not want, cause delegate can't inherit other parent class
you use generics, using somedelegate
type constraint:
class bb <u : somedelegate> { var delegate: u? = nil }
this way you'll need supply type of u
when initialise instance of bb
:
struct mystruct : somedelegate { // argument of xxx needs concrete type here. func xxx(x: int) { // ... } } let bb = bb<mystruct>()
Comments
Post a Comment