0

I'm trying to delete a tableView Cell with the NSFetchedResultsControllerDelegate. I'm using this if statement in the TabbleView commit function.

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        if let person = fetchedResultsController.object(at: indexPath){
            fetchedResultsController.managedObjectContext.delete(person)
        }
    }
}

This is the error I get: Initializer for conditional binding must have Optional type, not 'Person'

If I cast the person as an optinoal Person

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        if let person = fetchedResultsController.object(at: indexPath) as? Person{
            fetchedResultsController.managedObjectContext.delete(person)
        }
    }
}

I get this warning: Non-optional expression of type 'Person' used in a check for optionals

It seems to be working with the warning, but I would like to clear this up so that I better understand what is going on.

icekomo
  • 9,328
  • 7
  • 31
  • 59
  • 1
    `object(at:)` doesn't return an optional so you shouldn't be using it with an if let. – dan Dec 15 '16 at 19:08
  • Martin, where is the duplicate question, I did a search and couldn't find what you are referring to. – icekomo Dec 15 '16 at 19:20
  • Have a look at the top of your question: "This question already has an answer here: " ... followed by a link. You'll find more similar question in the "Related" section. – Martin R Dec 15 '16 at 19:22
  • Dan, what would I use, I tried objectAtIndexpath, but xcode just changes it to what I have above. – icekomo Dec 15 '16 at 19:22
  • Martin, Thanks somehow I didn't see that new section! – icekomo Dec 15 '16 at 19:24
  • Use what you were originally using, just use a regular `let` instead of an `if let` – dan Dec 15 '16 at 19:25

0 Answers0