首页 > 解决方案 > 条件绑定的初始化程序必须具有 Optional 类型,而不是“AnimationView”

问题描述

这是什么意思?

条件绑定的初始化程序必须具有 Optional 类型,而不是“AnimationView”

一直在努力弄清楚我的意思以及如何解决它?任何帮助将不胜感激

    override func viewDidLoad() {
    super.viewDidLoad()
            
    if let animationView = AnimationView(name: "breathing") {
        animationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
        animationView.center = self.view.center
        animationView.contentMode = .scaleAspectFill
        
        view.addSubview(animationView)
    
        animationView.play()
    }

标签: swiftuikitlottie

解决方案


该错误很明显AnimationView(name: "breathing")不是可选的。

代替:

if let animationView = AnimationView(name: "breathing") {

和:

let animationView = AnimationView(name: "breathing")

推荐阅读