Swift guard check for nil file - unexpectedly found nil

RanLearns

I'm trying to use a guard statement to check if this file is available or not.

guard UIImage(contentsOfFile: Bundle.main.path(forResource: imageName, ofType: "png")!) != nil else {
    print("\(imageName).png file not available")
    return
}

But I am getting a crash on the guard line with:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

imageName is not an optional. It is a String with a value.

nil is exactly what I'm trying to test for so why is the guard statement crashing?

rmaddy

Combining guard and forced-unwrapping is an oxymoron. One of the common uses of guard is guard let which safely guards against nil and eliminates the need to force-unwrap.

I would redo your code to something like:

guard let imagePath = Bundle.main.path(forResource: imageName, ofType: "png"), let image = UIImage(contentsOfFile: imagePath) else {
    print("\(imageName).png file not available")
    return
}

// Use image here as needed

If you don't actually need the image but you just want to make sure an image can be created, you can change that to:

guard let imagePath = Bundle.main.path(forResource: imageName, ofType: "png"), UIImage(contentsOfFile: imagePath) != nil else {
    print("\(imageName).png file not available")
    return
}

Having said all of that, if the image is actually supposed to be in your app bundle and it is simply a temporary issue such as forgetting to target the file properly, then don't use guard and go ahead and force-unwrap. You want the app to crash during development early on so you can fix the problem.

let image = UIImage(contentsOfFile: Bundle.main.path(forResource: imageName, ofType: "png")!)!

One last thing. You can more easily get the image using:

let image = UIImage(named: imageName)!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift unexpectedly found nil when there is actual value

Unexpectedly found 'nil' when running app on Swift

Simple UITableView in Swift - unexpectedly found nil

Swift SFSymbol unexpectedly found nil for UIImage

Unexpectedly found nil, but vars are not nil

Unexpectedly found nil

NSOutlineView unexpectedly found nil

Check for nil with guard instead of if?

Unexpectedly found nil while unwrapping optional value (Swift, Parse)

fatal error: unexpectedly found nil while unwrapping an Optional value swift

Swift 4 adding annotations error unexpectedly found nil while unwrapping

Swift: error: Unexpectedly found nil while implicitly unwrapping an Optional value

Unexpectedly found nil while unwrapping an Optional value swift

Swift : fatal error: unexpectedly found nil and EXC_BAD_INSTRUCTION

Swift: UIImageView - Unexpectedly found nil while unwrapping an Optional value

Catch any error, specially unexpectedly found nil in swift

Swift unit test - unexpectedly found nil while unwrapping an Optional value

Swift: “unexpectedly found nil while unwrapping an Optional value” in array append

Unexpectedly found nil while unwrapping an Optional value in Json parsing in swift

Swift Gyroscope: Unexpectedly found nil while unwrapping an Optional value

Swift unexpectedly found nil while unwrapping an optional value

Swift - Error: unexpectedly found nil while unwrapping an optional value

Swift addGestureRecognizer: Unexpectedly found nil while unwrapping an Optional value

Unexpectedly found nil while unwrapping an Optional value? Swift4

Swift3 : unexpectedly found nil while unwrapping an Optional value

fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

Swift unexpectedly found nil while unwrapping an Optional value

issue "unexpectedly found nil" with downloading image from firebase swift

Playing music in Swift: Unexpectedly found nil while unwrapping an Optional value