-1

When I try to unwrap "var window" using "!" it shows the Optional value is nil.

Fatal error: Unexpectedly found nil while unwrapping an Optional value

var window: UIWindow?

// image to be animated
let backgroundImg = UIImageView()


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // creating imageView to store background image "mainbg.jpg"
    backgroundImg.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
    backgroundImg.image = UIImage(named: "mainbg.jpg")
    self.window!.addSubview(backgroundImg)

    return true
}

Has anyone else faced this problem and managed to fix it?

I'm using XCODE Version 11.3.1 with SWIFT 5. This

Mathias
  • 41
  • 7
  • 2
    Does this answer your question? [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) – Joakim Danielson Feb 04 '20 at 20:21
  • 1
    If you're not using storyboards the window is not created for you, which is why it could be `nil`. If you are using storyboards, then you need to do some debugging and add your findings to your question. – EmilioPelaez Feb 04 '20 at 20:21

3 Answers3

0

You have to understand what an Optional type is. An optional type basically means that the variable can be nil .

To get the value from your variable if it is optional, you have to unwrap it. This just means putting an exclamation point at the end.

  • Thank you for the reply. I tried to force the unwrap using the "!" but as mentioned below, the var window is nil. – Mathias Feb 05 '20 at 12:41
0

As already mentioned in the comments, your UIWindow optional variable has not been assigned, so is still nil by default. You probably need a different project template that will set up the window for you.

As for the "!" operator, usually you should try to avoid using it, because if your optional is unassigned or nil, it will always throw an exception. Read about Optionals in the Swift Manual.

electromaggot
  • 634
  • 6
  • 16
0

The problem is the force unwrap on window, when you're trying to add the backgroundImg as subview. In the code that you shared, you don't set the window value before use that, so there is nil.

To fix it, you have to create a new window instance before adding the subview.

window = UIWindow()
self.window!.addSubview(backgroundImg)
  • How should I create the window instance before? I'm doing a Udemy course which uses exactly the same code but with an older version of XCode and Swift, but in that situation only declaring "window = UIWindow()" was enough to don't have the error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value" – Mathias Feb 05 '20 at 17:44
  • Yeah, in the `application(_:didFinishLaunchingWithOptions:)`, before add the subview you have to create the `UIWindow` instance, with `window = UIWindow()`- – Andrea Mengoli Feb 06 '20 at 11:22