冗余约束'Self':'AnyObject'

时间:2018-12-12 07:27:19

标签: ios swift uiviewcontroller

我有这个协议:

protocol Container: class where Self: UIViewController {
    var containerView: UIView! { get }
    var currentChild: UIViewController? { get set }
    func remove(child viewController: UIViewController)
    func add(child viewController: UIViewController)
    func replaceCurrentViewController(with newChild: UIViewController)
}

我面临的问题是它显示以下警告

  

冗余约束'Self':'AnyObject'

这是因为我同时使用了 class 其中的Self:UIViewController ,但是我需要两者!原因是我的协议扩展名(在下面找到),我使用UIViewController方法,如果删除 class ,我的扩展名将显示一条错误,要求添加 muting ,它应该没有,因为它只有一个类的协议。

extension Container {
    func remove(child viewController: UIViewController) {
        viewController.beginAppearanceTransition(false, animated: true)
        viewController.willMove(toParent: nil)
        viewController.removeFromParent()
        viewController.view.removeFromSuperview()
        viewController.endAppearanceTransition()
        currentChild = nil
    }

    func add(child viewController: UIViewController) {
        viewController.beginAppearanceTransition(true, animated: true)
        addChild(viewController)
        viewController.didMove(toParent: self)
        containerView.addSubview(viewController.view)
        viewController.view.frame = containerView.frame
        viewController.endAppearanceTransition()
        currentChild = viewController
    }

    func replaceCurrentViewController(with newChild: UIViewController) {
        if viewIfLoaded != nil, let currentChild = currentChild {
            if let parent = currentChild.parent, parent == self {
                remove(child: currentChild)
            }
            add(child: newChild)
        }
    }
}

那么,有没有更好的解决方案?我可以删除警告吗?

2 个答案:

答案 0 :(得分:2)

当您要访问下面的where方法时,实际上应该放置UIViewController子句,

protocol Container: class {
    var containerView: UIView! { get }
    var currentChild: UIViewController? { get set }
    func remove(child viewController: UIViewController)
    func add(child viewController: UIViewController)
    func replaceCurrentViewController(with newChild: UIViewController)
}

extension Container where Self: UIViewController {
    func remove(child viewController: UIViewController) {
        viewController.beginAppearanceTransition(false, animated: true)
        viewController.willMove(toParent: nil)
        viewController.removeFromParent()
        viewController.view.removeFromSuperview()
        viewController.endAppearanceTransition()
        currentChild = nil
    }

    func add(child viewController: UIViewController) {
        viewController.beginAppearanceTransition(true, animated: true)
        addChild(viewController)
        viewController.didMove(toParent: self)
        containerView.addSubview(viewController.view)
        viewController.view.frame = containerView.frame
        viewController.endAppearanceTransition()
        currentChild = viewController
    }

    func replaceCurrentViewController(with newChild: UIViewController) {
        if viewIfLoaded != nil, let currentChild = currentChild {
            if let parent = currentChild.parent, parent == self {
                remove(child: currentChild)
            }
            add(child: newChild)
        }
    }
} 

答案 1 :(得分:0)

swift 4 中,您可以使用

protocol Container where Self: UIViewController {
    var containerView: UIView! { get }
    var currentChild: UIViewController? { get set }
    func remove(child viewController: UIViewController)
    func add(child viewController: UIViewController)
    func replaceCurrentViewController(with newChild: UIViewController)
}

swift 5 中,您可以使用

protocol Container: UIViewController {
    var containerView: UIView! { get }
    var currentChild: UIViewController? { get set }
    func remove(child viewController: UIViewController)
    func add(child viewController: UIViewController)
    func replaceCurrentViewController(with newChild: UIViewController)
}