Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializer for conditional binding must have Optional type, not 'UIView'

From research on StackOverflow I've learned that this error is caused by attempting to bind a type that isn't an optional however it doesn't make sense in this situation because it used guard instead. Here's my code:

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

Xcode show an error on this line:

guard let containerView = transitionContext.containerView() else { return }
like image 664
Laurence Wingo Avatar asked Jun 29 '16 17:06

Laurence Wingo


2 Answers

transitionContext.containerView() was changed to return a non-optional, so you can't use it to initialize a variable in a conditional binding like a guard or if let.

You should remove the guard from that line:

let containerView = transitionContext.containerView()
like image 160
dan Avatar answered Nov 10 '22 21:11

dan


The container view in which a presentation occurs. It is an ancestor of both the presenting and presented view controller's views.

This containerView is being passed to the animation controller and It's return a non-optional

NOTE:

if let/if var optional binding only works when the result of the right side of the expression is an optional. If the result of the right side is not an optional, you can not use this optional binding. The point of this optional binding is to check for nil and only use the variable if it's non-nil.

like image 2
Ashish Avatar answered Nov 10 '22 23:11

Ashish