Swift UITableViewController运行时错误求助:解包Optional值遇nil
Hey there! Let's break down why you're hitting this crash and get your table view showing those images properly.
The Root Cause
Your crash is happening right here:
UIImage(contentsOfFile: "Megan.jpg")!
The UIImage(contentsOfFile:) method expects a full file system path to the image, not just the filename. Since you're passing only the name, it can't find the image file and returns nil. Then you force-unwrap that nil with !, which triggers the fatal error.
For images you've added directly to your Xcode project (like your .jpg files), you should use UIImage(named:) instead — this method automatically looks in your app's main bundle for the image resource, which is exactly what you need here.
Step-by-Step Fixes
Replace
contentsOfFilewithnamedand ditch the force-unwrap
You have two solid options here, depending on whether you want to allow missing images:Option 1: Use a default image if the main one is missing
If you want to show a fallback image (like a system person icon) when the requested image isn't found, use the nil-coalescing operator (
??):var mammaSitas : [MammaSita] = [ MammaSita(name: "Megan Fox", photo: UIImage(named: "Megan.jpg") ?? UIImage(systemName: "person.fill")!), MammaSita(name: "Scarlet Johanson", photo: UIImage(named: "Scarlet.jpg") ?? UIImage(systemName: "person.fill")!), MammaSita(name: "Bella Hadid", photo: UIImage(named: "Bella.jpg") ?? UIImage(systemName: "person.fill")!) ]Option 2: Make the
photoproperty optionalIf you're okay with showing no image when the file is missing, update your
MammaSitamodel to have an optionalUIImage?property, then initialize without force-unwrapping:// First, update your MammaSita class: class MammaSita { let name: String let photo: UIImage? // Change to optional type init(name: String, photo: UIImage?) { self.name = name self.photo = photo } } // Then update your array: var mammaSitas : [MammaSita] = [ MammaSita(name: "Megan Fox", photo: UIImage(named: "Megan.jpg")), MammaSita(name: "Scarlet Johanson", photo: UIImage(named: "Scarlet.jpg")), MammaSita(name: "Bella Hadid", photo: UIImage(named: "Bella.jpg")) ]Your table view cell code will work as-is, since
cell.imageView?.imageaccepts an optionalUIImage.Double-check your image setup
Even with the right code, images won't load if they're not properly added to your project:- Make sure the image files are in your Xcode project navigator (not just sitting in your Finder folder).
- Select each image file, then in the File Inspector (right sidebar), confirm the Target Membership checkbox for your app is checked.
- Verify the filename matches exactly (iOS is case-sensitive on real devices — "megan.jpg" != "Megan.jpg").
Why This Works
UIImage(named:) handles all the heavy lifting of finding the image in your app's bundle, so you don't have to mess with file paths. And by avoiding ! force-unwraps, you eliminate the crash risk entirely — either you get the image, or you use a fallback, or you just show nothing (no crash either way).
内容的提问来源于stack exchange,提问作者edward

