简体   繁体   中英

Swift 2 Function enforcing Class and Protocol adherence, Generic parameter could not be inferred

I have the following code to let me switch away from any view controller to any other one, and the parameter needs to be an NSViewController that conforms to the Delegatable protocol.

When I call the function on line 3, I get Generic parameter 'C' could not be inferred .

func foo() {
    var viewController = CustomViewController()
    showViewForController(&viewController)
}

private func showViewForController<C:NSViewController where C:Delegatable>(inout viewController: C) -> Void {
    currentVC?.removeFromParentViewController()
    currentVC?.view.removeFromSuperview()

    viewController.delegate = self
    addChildViewController(viewController)
    self.view.addSubview(viewController.view)
    currentVC = viewController
}

Your CustomViewController doesn't conform to Delegatable, which would give that compiler error when trying to pass it in to your function. The error message isn't exactly helpful though.

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