gpt4 book ai didi

swift - 转换为 Swift 3 语法时出现错误 "Optional type"

转载 作者:行者123 更新时间:2023-11-28 16:11:13 25 4
gpt4 key购买 nike

在将我的项目转换为 Swift 3 语法时,我遇到了几个错误,但除了一个错误之外,我能够解决所有错误。

我得到的错误信息是:

“条件绑定(bind)的初始化器必须具有可选类型,而不是‘UIView’”

这是代码(我使用的是 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")
}

如果失败了:

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

错误信息出现在这里:

let containerView = transitionContext.containerView else {
return
}

我还不是很擅长 swift,显然还不足以解决这个错误。我很感激能得到的所有帮助!

最佳答案

transitionContext.containerView 不是可选的,您不需要将它放在 guard-let 语句中,因为它总是有一个值

替换这个:

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

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

关于swift - 转换为 Swift 3 语法时出现错误 "Optional type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39529157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com