0

I try to add an swipe to edit option in my TableView. If the User press Edit, then a new View should open.

But I always get an error in the func "editActionsForRowAt" on the line with "if segue.identifier == "ItemDetailsVS {" "": fatal error: unexpectedly found nil while unwrapping an Optional value

The segue have the correct Name in the Storyboard. Any ideas?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ItemDetailsVC" {
        if let destination = segue.destination as? ItemDetailsViewController {
            if let item = sender as? Item2 {
                destination.itemToEdit = item
            }
        }
    }
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let edit = UITableViewRowAction(style: .normal, title: "Edit"){ (action, indexPath) in

        var segue: UIStoryboardSegue!

        if segue.identifier == "ItemDetailsVC" {
            if let objects = self.controller.fetchedObjects , objects.count > 0 {
                let item = objects[indexPath.row]
                self.performSegue(withIdentifier: "ItemDetailsVC", sender: item)
                }
            }
        }
    return [edit]

}
ma09111
  • 33
  • 1
  • 6
  • 1
    You are creating an empty variable and use it, of course it will yell error for you, you can refer to [this](http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift) to learn how to use segue – Tj3n Nov 21 '16 at 11:39

1 Answers1

0

It's because you are using Segue Object which is not initialised to do so use "?" instead of "!".Also check the identifier.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

Ajay Odedra
  • 3
  • 1
  • 2