2016-09-16 2 views
0

Bei der Konvertierung meines Projekts in die Swift 3-Syntax bin ich auf mehrere Fehler gestoßen und konnte alle bis auf einen Fehler beheben.Fehler "Optionaler Typ" beim Konvertieren in die Swift 3-Syntax

Die Fehlermeldung Ich erhalte ist:

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

Hier ist der Code (Ich verwende Yalatis cocoapod ColorMatchTabs):

public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
    self.transitionContext = transitionContext 

    guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to), 
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from), 
     let containerView = transitionContext.containerView else { 
     return 
    } 
    containerView.addSubview(toViewController.view) 

    let needShow = mode == .show 
    if !needShow { 
     containerView.addSubview(fromViewController.view) 
    } 

    let animatedViewController = needShow ? toViewController : fromViewController 
    let initialRect = CGRect(origin: startPoint, size: CGSize.zero) 
    let initialCircleMaskPath = UIBezierPath(ovalIn: initialRect) 
    let extremePoint = CGPoint(x: startPoint.x, y: animatedViewController.view.bounds.height) 
    let radius = hypot(extremePoint.x, extremePoint.y) 
    let finalCircleMaskPath = UIBezierPath(ovalIn: initialRect.insetBy(dx: -radius, dy: -radius)) 

    let maskLayer = CAShapeLayer() 
    maskLayer.path = needShow ? finalCircleMaskPath.cgPath : initialCircleMaskPath.cgPath 
    animatedViewController.view.layer.mask = maskLayer 

    let maskLayerAnimation = CABasicAnimation(keyPath: "path") 
    maskLayerAnimation.fromValue = initialCircleMaskPath.cgPath 
    maskLayerAnimation.fromValue = needShow ? initialCircleMaskPath.cgPath : finalCircleMaskPath.cgPath 
    maskLayerAnimation.toValue = needShow ? finalCircleMaskPath.cgPath : initialCircleMaskPath.cgPath 

    maskLayerAnimation.duration = transitionDuration(using: transitionContext) 
    maskLayer.add(maskLayerAnimation, forKey: "path") 
} 

Dies ist waren versagt es:

guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to), 
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from), 
     let containerView = transitionContext.containerView else { 
     return 
    } 

Und die Fehlermeldung erscheint hier:

Ich bin noch nicht sehr gut in schnelle, offensichtlich nicht gut genug, um diesen Fehler zu beheben. Ich bin dankbar für all die Hilfe, die ich bekommen kann!

Antwort

1

transitionContext.containerView ist nicht optional, Sie nicht innerhalb einer Wache lassen Anweisung setzen müssen, weil es immer einen Wert

das Ersetzen haben:

guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to), 
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from), 
    let containerView = transitionContext.containerView else { 
    return 
} 

mit

guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to), 
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) else { 
    return 
} 
let containerView = transitionContext.containerView 
+0

Vielen Dank! Arbeitete perfekt! –

Verwandte Themen