So I'm following through with project 5 and have followed closely until the end of the first section of part 2. When I compile and run, I run into the error "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update". This error occurs only after I click Submit to enter my answer so that it can be inserted at row: 0, section: 0. I have double checked my code and can not find a difference between mine and the one Paul provides.
Here is the code for my submit function
func submit(_ answer: String) {
let lowerAnswer = answer.lowercased()
if isPossible(word: lowerAnswer) {
if isOriginal(word: lowerAnswer) {
if isReal(word: lowerAnswer) {
usedWords.insert(answer, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic) //the exact line that seems to be causing my crash
}
}
}
}
Here is the code for my promptForAnswer function as well, in case that is of use:
@objc func promptForAnswer() {
let ac = UIAlertController(title: "Enter answer", message: nil, preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) {
[weak self, weak ac] action in
guard let answer = ac?.textFields?[0].text else { return }
self?.submit(answer)
}
ac.addAction(submitAction)
present(ac, animated: true)
}