Swift: Generics & downcast to AnyObject -
i have following situation: (just copy playground)
import cocoa protocol uid { var uid: int {get set} } class : uid { var uid = 01 } class manageuid<t: uid> { let cache = nscache() func adduid(aobj: t) { cache.setobject(aobj, forkey:aobj.uid) } func deleteduid(aobj: t) { cache.removeobjectforkey(aobj.uid) } }
this lead error "could not find overload setobject ..."
well know problem type of aobj in adduid, not clear compiler.
on way say
func adduid(aobj: t) { cache.setobject(aobj a, forkey:aobj.uid) }
but in case can skip generic topic completely. try downcast aobj anyobject or nsobject did not work.
any ideas?
i nice have generic version of cache :)
update:
on solution add @objc attribute on protocol, therefore protocol usable classes lead general restriction protocol. local solution in class manageuid.
i found solution, primary derived answer of question here: https://devforums.apple.com/thread/233432
protocol uid { var uid: int {get set} } @class_protocol protocol objectuid : uid { } class manageuid<t: objectuid> { let cache = nscache() func adduid(aobj: t) { cache.setobject(aobj, forkey:aobj.uid) } func deleteduid(aobj: t) { cache.removeobjectforkey(aobj.uid) } }
Comments
Post a Comment