0

I have some problem with this error:

unexpectedly found nil while unwrapping

Debugger show me the place of this error but I do not know what is wrong. Error is on the UIView animation block function. I have a problem with recognize the problem. There is a code:

override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self,
                                                             selector: #selector(playerItemDidReachEnd),
                                                             name: AVPlayerItemDidPlayToEndTimeNotification,
                                                             object: self.player?.currentItem);
}

func playerItemDidReachEnd(notification: NSNotification) {
    self.player?.seekToTime(kCMTimeZero)
    self.player?.play()

    self.freeEffectAnimationImage.layer.removeAllAnimations()
    self.freeEffectAnimationImage.center = self.beginingPoint
    self.freeEffectAnimationImage.stopAnimating()
    self.freeEffectAnimationImage.startAnimating()

    if beginingPoint.x < 15 {
        self.beginingPoint = CGPointMake(-CGRectGetWidth(freeEffectAnimationImage.frame), beginingPoint.y)
    }

    if endingPoint.x > self.view.frame.size.width - 15 {
        self.endingPoint = CGPointMake(self.view.frame.size.width + CGRectGetWidth(freeEffectAnimationImage.frame), endingPoint.y)
    }
    UIView.animateWithDuration(self.animationDuration/2, delay:freeEffectDelay, options:UIViewAnimationOptions.Autoreverse, animations: {
        self.freeEffectAnimationImage.center = self.endingPoint
        }, completion: {
            (success:Bool) in
            if success {
                self.freeEffectAnimationImage.center = self.beginingPoint
            }
    })
}

I will be glad for help :)

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
Dzeremix
  • 446
  • 1
  • 5
  • 24
  • did you debug to see what the values are? – Wain Jun 08 '16 at 10:20
  • The provided codes doesn't show how `beginingPoint` and `endingPoint` are initialized, uniess their values meet a certain criteria (the if block). What is the value immediately before calling `UIView.animation` – DJohnson Jun 08 '16 at 10:27
  • Is freeEffectAnimationImage, self.beginingPoint or self.endingPoint an optional? if so try self. freeEffectAnimationImage?.center – Nagra Jun 08 '16 at 10:48
  • Which line is causing the error? – kennytm Jun 08 '16 at 10:49
  • Where do you set up the `freeEffectAnimationImage` ? an educated guess would be that it has not been initialized when you receive the notification... – T. Benjamin Larsen Jun 08 '16 at 10:58
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Mar 19 '18 at 06:03

1 Answers1

1

An educated guess would be that your self.freeEffectAnimationImage has yet to be created when the AVPlayerItemDidPlayToEndTimeNotification-notification is received.

So, when the animation-block tries to access the expected instance it finds nil instead and dies...

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32