Error with CoreData in viewDidLoad()

I’m getting an error within my “viewDidLoad()” function. It is line 57 and the error after the app crashes reads

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

The source code is here:

import UIKit
import CoreData
var intentionsArray: [NSManagedObject] = []
class ViewController: UIViewController, UINavigationControllerDelegate, UITableViewDataSource {
   
    @IBOutlet weak var intentionsTable: UITableView!
    @IBOutlet weak var intentionField: UITextField!
   
   
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let intentionInd = intentionsArray[indexPath.row]
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = intentionInd.value(forKey: "eIntentionCell") as? String
        return (cell)
    }
   
   
    public func tableView(_ intentionsTable: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (intentionsArray.count)
    }
   
    /
    @IBAction func addIntention(_ sender: AnyObject) {
       
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
       
        /
        let newIntention = NSEntityDescription.insertNewObject(forEntityName: "EIntentions", into: context)
       
        newIntention.setValue(intentionField.text, forKey: "eIntentionCell")
       
        do{
            try context.save()
            intentionField.text = ""
        } catch {
            print("Error")
        }
    }
   
    @IBAction func refreshTable(_ sender: Any) {
        intentionsTable.reloadData()
    }
   
    override func viewDidLoad() {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let intRequest = NSFetchRequest(entityName: "EIntentions")
        intRequest.returnsObjectsAsFaults = false
       
        do{
            let intentionResults = try context.fetch(intRequest)
            if intentionResults.count > 0 {
                for intResult in intentionResults as! [NSManagedObject] {
               
                    if let intent = intResult.value(forKey: "eIntentionCell") as? String {
                        intentionsArray.append(intent as! NSManagedObject)
                    }
                }
            }
        }catch{
            print("Error")
        }
       
       
       
    }
   
}
  

 

Any idea what might be causing this crash?

Powered by WPeMatico

About

You may also like...

Comments are closed.