Swift declare that AnyClass object implements protocol -
i writing framework creating plugins os x application. these plugins loaded nsbundle
s, , bundles' principalclass
es used run plugin code. however, can't figure out how declare principalclass
object conforms protocol.
let bundles = cfbundlecreatebundlesfromdirectory(kcfallocatordefault, nsurl(fileurlwithpath: bundledirectory), "bundle") nsarray bundle in bundles { let bundleclass = bundle.principalclass!! if (bundleclass.conformstoprotocol(myprotocol.self)) { //produces compiler error: //"cannot downcast 'anyclass' non-@objc protocol type 'myprotocol'" let loadedclass = bundleclass myprotocol } }
what proper syntax declaring conforms protocol? (also, protocol declared @objc
i'm not sure why says "non-@objc" protocol.)
try: bundleclass myprotocol.protocol
. docs here.
btw, code can simplified
let bundles = cfbundlecreatebundlesfromdirectory(kcfallocatordefault, nsurl(fileurlwithpath: bundledirectory), "bundle") nsarray bundle in bundles { if let loadedclass = bundle.principalclass as? myprotocol.protocol { // ... } }
Comments
Post a Comment