How to check/cast class to generic type with where in Swift -
i came with simple playground illustrating problem:
import uikit protocol myprotocol { var foo: bool { set } } class mygenericclass<t: uiview t: myprotocol>: uiview {} func checkifismygenericclass(view: uiview) -> bool { return view mygenericclass // generic parameter 't' not inferred }
i need identify instances of mygenericclass
.
my actual code isn't simple, please don't ask me change mygenericclass
declaration.
mygenericclass has associatedtype requirements. not sure try
import uikit protocol myprotocol { var foo: bool { set } } class mygenericclass<t: uiview t: myprotocol>: uiview {} func checkifismygenericclass<t: uiview t: myprotocol>(view: t) -> bool { return view mygenericclass<t> // generic parameter 't' not inferred }
updated
import uikit protocol myprotocol { var foo: bool { set } } class view: uiview, myprotocol { var foo: bool = true } class mygenericclass<t: uiview t: myprotocol>: uiview { var variable: t! init() { super.init(frame: cgrectzero) } } let view = view() let obj = mygenericclass<view>() obj.variable = view let anyotherobj = uiview() func checkifismygenericclass<t: uiview t: myprotocol>(view: uiview, type: t) -> bool { return view mygenericclass<t> } checkifismygenericclass(obj, type: view) // returns true checkifismygenericclass(anyotherobj, type: view) // returns false
Comments
Post a Comment