Error when using coredata

I'm trying to use core data to indicate the date and time, but I'm getting an error.

do {
  let out = try context.fetch(name)
   
  for data in out as! [NSManagedObject] {
    //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    let day = data.value(forKey: "day") as! String
    let time = data.value(forKey: "time") as! String

    let id = data.value(forKey: "id") as! String
    let title = data.value(forKey: "title") as! String
     
    self.datas.append(MainViewModel(id: id, day: day, title: title, time: time))
  }
}
catch {
  print(error.localizedDescription)
}

}

The cause of the error you posted is well documented, please look it up. In fact, the line of code causing the issue should've been highlighted in the Xcode debugger when your program crashed, which should provide a starting point for your investigation. Additionally, please don't just post code and expect people to do something with it. Ask a specific question. Finally, if you have a question about some specific code, please include only that code, not all surrounding code that may not be relevant. Posters will ask for additional context if they can't answer your question with the code sample provided.

Terms of Service

Privacy Policy

Cookie Policy