gpt4 book ai didi

ios - 冗余约束 'Self' : 'AnyObject'

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:36 25 4
gpt4 key购买 nike

我有这个协议(protocol):

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)
}

我遇到的问题是显示如下警告

Redundant constraint 'Self' : 'AnyObject'

这是因为我同时使用了 classwhere Self: UIViewController,但我需要两者!原因在于我的协议(protocol)扩展(见下文),我使用 UIViewController 方法,如果我删除 class,我的扩展会显示一个错误,要求添加 mutating,这它不应该有,因为它只是一个类协议(protocol)。

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)
}
}
}

那么,有没有更好的解决办法呢?我可以删除警告吗?

最佳答案

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)
}

关于ios - 冗余约束 'Self' : 'AnyObject' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53738001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com