-1

This code is causing my application to crash and I can't figure out a way to fix it. The error is : fatal error: unexpectedly found nil while unwrapping an Optional value

Any idea how I can fix it? The two lines I'm returning in my createCharacters() function is whats causing the crash:

class NACharacters {

var featuredImage : UIImage!

init(featuredImage: UIImage){

    self.featuredImage = featuredImage

}

static func createCharacters() -> [NACharacters]{

    return[
        //THE TWO LINES BELOW CAUSE THE CRASH
        NACharacters(featuredImage: UIImage(named: "Diplo Squad")!),
        NACharacters(featuredImage: UIImage(named: "StopIcon")!)
    ]
  }
}

Solution: I simply needed to delete the space between "Diplo" and "Squad". It seems this was returning nil.

OriginalAlchemist
  • 361
  • 1
  • 6
  • 25

2 Answers2

0

check if your images "Diplo Squad" und "StopIcon" exist.

(You may need to remove the space in the first image name)

At least one of these UIImage(name: "...") calls returns nil and that's probably the crash reason.

Dominik Vincenz
  • 445
  • 2
  • 10
0

The only thing that could be nil lines are in the UIImages. check that they exist in your projectNavigator or assets.

Akshansh Thakur
  • 4,558
  • 1
  • 16
  • 37