why it says "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"?

I have IOS projets, it work but suddenly crash and throw error like "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" Why?

import Foundation
import AVFoundation

struct Video : Identifiable {
  var id = UUID()
  var player : AVPlayer
  var user: User
}

struct User: Identifiable {
  var id = UUID()
  let userName: String
  let userImage: String
}

struct MockData {

  let videos: [Video] = [
    Video(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "reel_1", ofType: "mp4")!)),

       user: User(userName: "cristiano", userImage: "user_9")),
    Video(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "reel_2", ofType: "mp4")!)),
       user: User(userName: "mann_daar", userImage: "user_3")),
]
}
Answered by ForumsContributor in
Accepted Answer

The most likely reason is that one of your mp4 files can't be found in the app bundle:

  • reel_1.mp4
  • reel_2.mp4

Because you are force-unwrapping your file paths, a failure will result in a crash.

Bundle.main.path(forResource: "reel_1", ofType: "mp4")

...returns an Optional, so if you use ! on it, and the file can't be found, the app will crash.

why it says "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"?
 
 
Q