条件绑定的初始化程序必须具有可选类型,而不是'UIView'

Cos*_*ows 11 optional ios swift3

通过对StackOverflow的研究,我了解到这个错误是由于尝试绑定一个不是可选的类型引起的,但是在这种情况下没有意义,因为它使用了guard.这是我的代码:

func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) {
        // Here, we perform the animations necessary for the transition

        guard let fromVC = transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey) else { return }
        let fromView = fromVC.view
        guard let toVC = transitionContext.viewController(forKey: UITransitionContextToViewControllerKey) else { return }
        let toView = toVC.view
        guard let containerView = transitionContext.containerView() else { return }

        if self.presenting {
            containerView.addSubview(toView)
        }

        let animatingVC = self.presenting ? toVC : fromVC
        let animatingView = animatingVC.view
        let appearedFrame = transitionContext.finalFrame(for: animatingVC)

        var alpha: CGFloat = 1
        if self.options.contains([.AlphaChange]) {
            alpha = 0;
        }

        let initialAlpha = self.presenting ? alpha : 1
        let finalAlpha = self.presenting ? 1: alpha

        var dismissedFrame = appearedFrame
        let startRect = CGRect(origin: appearedFrame.origin, size: containerView.bounds.size)
        let offset = self.calculateStartPointOffset(startRect, options: self.options)

        if options.contains([.Dissolve]) && !self.presenting {
            dismissedFrame.size = containerView.bounds.size
            dismissedFrame.origin = CGPointZero
        } else {
            dismissedFrame = CGRect(x: offset.x, y: offset.y, width: appearedFrame.width, height: appearedFrame.height)
        }

        let initialFrame = self.presenting ? dismissedFrame : appearedFrame
        let finalFrame = self.presenting ? appearedFrame : dismissedFrame
        animatingView?.frame = initialFrame
        animatingView?.alpha = initialAlpha
        let dumpingValue = CGFloat(self.options.contains([.Interactive]) ? 1 : 0.8)

        UIView.animate(withDuration: self.transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: dumpingValue, initialSpringVelocity: 0.2, options: [UIViewAnimationOptions.allowUserInteraction, UIViewAnimationOptions.beginFromCurrentState],
                                   animations:
            { () -> Void in
                animatingView?.frame = finalFrame
                animatingView?.alpha = finalAlpha
            })
        { (completed) -> Void in
                if !self.presenting {
                    fromView?.removeFromSuperview()
                    self.tDelegate?.didDissmisedPresentedViewController()
                }
                let cancelled = transitionContext.transitionWasCancelled()
                transitionContext.completeTransition(!cancelled)
        }
    }
Run Code Online (Sandbox Code Playgroud)

Xcode在此行显示错误:

guard let containerView = transitionContext.containerView() else { return }
Run Code Online (Sandbox Code Playgroud)

dan*_*dan 18

transitionContext.containerView()更改为返回非可选,因此您不能使用它来初始化条件绑定中的变量,如guardif let.

你应该删除该guard行:

let containerView = transitionContext.containerView()
Run Code Online (Sandbox Code Playgroud)