Hello,
I need help with “Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value”
I have this NavigationController on my Storyboard:
RoomTableViewController (with all my items:room-entity) goes to → DetailViewController with details of the row from room-entity and a separate DayTableView inside to select → one day at the WeekTableViewController as a relationship.
In detail:
I have an item (room) and I want to assign a (day) - I worked with the BowTie and Device Relationship video-example. If I add and save my new item (room) go back to the RoomTableViewController, than select the row and want to assign in this second step the “day” everything is OK and worked for me.
But I want to do all in one step
→ Add the new room and assign the day from the DayTableViewController.
At this point I earn the “ERROR” = myRoom.weekday_rls = wochentag If I want to do it in one step it is nil, I I do the same in the two steps I can save the room and assign the day crazy
extension AddRoomViewController : DayPickerDelegate {
func didSelectDay (wochentag: Wochentag){
do {
myRoom.weekday_rls = wochentag
try fetchedResultsCtrl.managedObjectContext.save()
print("Picker gespeichert")
print(wochentag)
} catch {
print("Error saving todo: \(error)")
}
}
}
Some more code from my project:
lazy var appDelegate = UIApplication.shared.delegate as! AppDelegate
var managedContext = (UIApplication.shared.delegate as! AppDelegate).managedContext
var myRoom: Raumliste!
lazy var fetchedResultsCtrl: NSFetchedResultsController<Raumliste> = {
let request: NSFetchRequest<Raumliste> = Raumliste.fetchRequest()
let sort = NSSortDescriptor(key: "name", ascending: true)
request.sortDescriptors = [sort]
let fetchedCtrl = NSFetchedResultsController(fetchRequest: request, managedObjectContext: self.managedContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedCtrl.delegate = self
return fetchedCtrl
}()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
saveDataBase()
if indexPath.section == 1 && indexPath.row == 0{
if let weekDayPicker =
storyboard?.instantiateViewController(withIdentifier: dayCell) as? WochentagController {
weekDayPicker.managedContext = self.fetchedResultsCtrl.managedObjectContext
//Week Setup
weekDayPicker.dayPickerDelegate = self
weekDayPicker.selectedDay = myRoom?.weekday_rls
navigationController?.pushViewController(weekDayPicker, animated: true)
}
}
tableView.deselectRow(at: indexPath, animated: true)
}
Why I got the crash? Why Ii is nil?