简体   繁体   中英

Casting a viewController extracted from an UINavigationController in Swift

I have UIViewController in a UINavigationController . This view controller implement a protocol.

I'm trying to get the first controller of the navigation stack and to cast it :

if let vc = _navigationStack.viewControllers?[0] as? ExplorerInterface {
    return vc
}

but I get the error : Cannot downcast from 'AnyObject?' to non-@objc protocol type 'ExplorerInterface' Cannot downcast from 'AnyObject?' to non-@objc protocol type 'ExplorerInterface' .

I'm looking for a way to fix this issue without adding the @objc keyword to my protocol (because this protocol contain swift type like tuples and the @objc keyword is not compatible with Swift types)

Form Apple Doc:

You can check for protocol conformance only if your protocol is marked with the @objc attribute. Even if you are not interoperating with Objective-C, you need to mark your protocols with the @objc attribute if you want to be able to check for protocol conformance.

Check Swift Protocols

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM