Search code examples
swiftuikitlottie

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


What does this mean?

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

been struggling to figure out what i means and how to fix it? any help would be appreciative

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

Solution

  • The error is pretty clear AnimationView(name: "breathing") isn't an optional.

    Replace:

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

    With:

    let animationView = AnimationView(name: "breathing")