Ios 条件绑定的初始值设定项必须具有可选类型,而不是';UIView&x27;
通过对StackOverflow的研究,我了解到这个错误是由于试图绑定一个不是可选的类型造成的,但是在这种情况下它没有意义,因为它使用了guard。这是我的密码:Ios 条件绑定的初始值设定项必须具有可选类型,而不是';UIView&x27;,ios,optional,swift3,Ios,Optional,Swift3,通过对StackOverflow的研究,我了解到这个错误是由于试图绑定一个不是可选的类型造成的,但是在这种情况下它没有意义,因为它使用了guard。这是我的密码: func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) { // Here, we perform the animations necessary for the transition gua
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在此行上显示错误:
guard let containerView = transitionContext.containerView() else { return }
transitionContext.containerView()
已更改为返回非可选变量,因此您不能使用它初始化条件绑定中的变量,如guard
或if let
您应该从该行中移除防护装置
:
let containerView = transitionContext.containerView()
出现演示文稿的容器视图。它是呈现和呈现视图控制器视图的祖先 此containerView被传递到动画控制器,并返回一个非可选的 注:
if-let/if-var可选绑定仅在表达式右侧的结果为可选时工作。如果右侧的结果不是可选的,则不能使用此可选绑定。此可选绑定的目的是检查nil,并且仅在变量为非nil时使用该变量。显示了哪个错误?谢谢。从保护条件中删除这段代码后,它现在可以工作了。使用上面建议的行,我得到了另一个错误,“不能调用非函数类型UIView的值;修复它:Delete()”有什么想法吗?