'Receiving Thread 1: EXC_BAD_ACCESS (code=1, address=0x6b49dedf6a0) Error

I am trying to build a simple To Do app with Core Data. In CoreData Model, I have created an Entity named "ToDoCoreData". It has one attribute "newToDoItem" type of String.

In CellForRowAt method I'm receiving the following error Thread 1: EXC_BAD_ACCESS (code=1, address=0x6b49dedf6a0) - but everytime I rerun the app the newly added ToDo appeares in row.

The code is below. Would be glad if someone could help, please.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    
    var toDoList:[ToDoCoreData] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        
        let fetchRequest: NSFetchRequest<ToDoCoreData> = ToDoCoreData.fetchRequest()
        
        do {
            toDoList = try context.fetch(fetchRequest)
        } catch {
            print(error.localizedDescription)
        }
    }
    
    @IBAction func addNewToDo(_ sender: UIBarButtonItem) {
        let ac = UIAlertController(title: "Add Task", message: "add New Task", preferredStyle: .alert)
        
        let ok = UIAlertAction(title: "Add", style: .default) { (action) in
            let textField = ac.textFields?[0]
//            self.toDoList.insert((textField?.text)!, at: 0)
            self.saveToDo(newToDoItem: (textField?.text)!)
            self.tableView.reloadData()
        }
        
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        ac.addTextField { textfield in
            
        }
        
        ac.addAction(ok)
        ac.addAction(cancel)
        present(ac, animated: true, completion: nil)
    }
    
    func saveToDo(newToDoItem: String) {
        
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        
        let entity = NSEntityDescription.entity(forEntityName: "ToDoCoreData", in: context)
        let taskObject = NSManagedObject(entity: entity!, insertInto: context) as! ToDoCoreData
        taskObject.newToDoItem = newToDoItem
        
        do {
            try context.save()
            toDoList.append(taskObject)
            print("Success!")
        } catch {
            print(error.localizedDescription)
        }
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        toDoList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        
        let toDo = toDoList[indexPath.row]
        cell.rowLabel.text = toDo.newToDoItem
        return cell
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source