0

i have been getting this error fatal error:

unexpectedly found nil while unwrapping an Optional value.

In this code

class ShimmerView: NSObject {

    // weak propeties
    weak var animatedView : UIView!

    // Strong Properties
    var shadowBackgroundColor : UIColor!
    var shadowForegroundColor : UIColor!
    var shadowWidth : CGFloat!
    var repeatCount : CGFloat!
    var duration : NSTimeInterval!

    var currentAnimation : CABasicAnimation!

    func commonInit(){
      self.shadowBackgroundColor = UIColor(white: 1, alpha: 0.3)
      self.shadowForegroundColor = UIColor.whiteColor()

      self.shadowWidth = 20
      self.repeatCount = CGFloat.infinity
      self.duration = 3.0
    }

    func start(){

        if(self.animatedView == nil){
          print("ShimmerView has nothing to return")
          self.stop()
        }

        let gradientMask = CAGradientLayer()
        gradientMask.frame = self.animatedView.bounds

        let gradientSize : CGFloat = self.shadowWidth / self.animatedView.frame.size.width

        let startLocations : NSArray = [0,Int(gradientSize / 2.1),Int(gradientSize)]
        let endLocations : NSArray = [Int(1.0 - gradientSize) , Int(1.0 - (gradientSize / 2.0)) , 0]
        // The error is on this line
        gradientMask.colors = [self.shadowBackgroundColor.CGColor , self.shadowForegroundColor.CGColor , self.shadowBackgroundColor.CGColor]
        gradientMask.locations = startLocations as? [NSNumber]
        gradientMask.startPoint = CGPointMake(0 - (gradientSize * 2), 0.5)
        gradientMask.endPoint = CGPointMake(1 + gradientSize, 0.5)

        self.animatedView.layer.mask = gradientMask

        currentAnimation = CABasicAnimation(keyPath: "locations")
        currentAnimation.fromValue = startLocations
        currentAnimation.toValue = endLocations
        currentAnimation.repeatCount = Float(self.repeatCount)
        currentAnimation.duration = self.duration
        currentAnimation.delegate = self

        gradientMask.addAnimation(currentAnimation, forKey: "ShimmerView")   
    }

i have debugged but can't really find nothing related to the error i have written in the code where the error occurs

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Aryan Kashyap
  • 157
  • 3
  • 11

1 Answers1

0

You are declaring a lot of your variables as implicitly unwrapped optionals (using ! after the type of the variable), for example:

weak var animatedView : UIView!

From Apple's documentation "Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter. The primary use of implicitly unwrapped optionals in Swift is during class initialization"

From your code I cant really see the animatedView variable being initialized, swift will throw an error if your variables (that are not declared as optionals) are not initialized; However, this is not the case for implicitly unwrapped optionals as they are assumed to be available as soon as the class is initialized.

I believe your problem might be related to this, you can set a breakpoint before the line that's throwing the error and find out which of your optionals is nil. I see that animatedView is a weak variable, is there a good reason for this? your view could be getting disposed at some point.

Cheers.

Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30