I have this 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)
}
the problem I am facing is that it shows the following warning
Redundant constraint 'Self' : 'AnyObject'
this is because I am using both class & where Self: UIViewController, but I need both! the reason for that is in my protocol extension (found below), I use UIViewController methods, and if I delete class, my extension shows an error asking for adding mutating, which it should not have because its a class only 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)
}
}
}
so, is there a better solution? can I remove the warning?
In swift 4 you can use
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)
}
In swift 5 you can use
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With