How to declare protocol constrainted generic variable in Swift? -
i need create dictionary variable value of someotherclass class , conforms someprotocol. equals following declaration in objective-c:
nsmutabledictionary<someclass *, someotherclass<someprotocol> *> *someotherbysome; so, possible in same simple way using swift?
i need because can create func signature:
func somefunc<t: someotherclass t: someprotocol>(param: t, otherparam: string) and want values param parameter dictionary without type casting.
short answer
in swift cannot declare variable of given class and conform protocol.
possible solution (?)
however given definitions have
class someclass: hashable { var hashvalue: int { return 0 } // logic goes here } func ==(left:someclass, right:someclass) -> bool { return true // logic goes here } class someotherclass {} protocol someprotocol {} maybe can solve problem making someotherclass conform someprotocol
extension someotherclass: someprotocol { } now can simply
var dict : [someclass: someotherclass] = [someclass(): someotherclass()] let someprotocolvalue: someprotocol = dict.values.first! does you?
Comments
Post a Comment