Core Data TableView Display Problem

I am trying to display the application Core Data values in a separate tableView, where I have the following in Storyboard:

  1. I have a main window controller / view controller.
  2. A seque from the main window view controller to a separate data INPUT window controller / view controller.
  3. A seque from the main window view controller to a separate TABLEVIEW window controller / view controller.
  4. Buttons to activate each separately.

The Core Data input stores properly, but the tableView fails to exhibit the fetched data. The tableView ViewDidLoad properly identifies the values, the TableViewDataSource indicates an array count (most likely the faults count), and the TableViewDelegate indicates the NSManagedObject does not have a member name calendarDate. Therefore, an error exists, and the tableView does not display the values.

So, I am obviously missing the proper method to display the Core Data Values. I am confused as to why ViewDidLoad can see the fetched array values, but the tableView functions cannot. Your guidance as to what I am doing incorrectly within the functions are most welcome. Just another step in learning Core Data with your assistance ... :]

My tableView code is listed below with comments.

Best regards,

jim_k


import Cocoa
import CoreData

class MyTableViewViewController: NSViewController {

//  MARK: - IBOutlet Properties

    @IBOutlet var tableView: NSTableView!
    @IBOutlet var statusLabel: NSTextField!

    /// Identify the Application's Variable "Core Data NSManagedObject Array"
    var theRequiredDataMutableArray: [NSManagedObject] = []

// MARK: - View Did Load

    override func viewDidLoad() {
        super.viewDidLoad()
        print("ViewDidLoad came first.")
        guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return }
        let theManagedObjectContext = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "MyNumberDataRegistration")
        do {
            theRequiredDataMutableArray = try theManagedObjectContext.fetch(fetchRequest)
            if theRequiredDataMutableArray.count > 0 {
                for theIdentifiedItem in theRequiredDataMutableArray {
                    print(theIdentifiedItem.value(forKey: "calendarDate")!)
                }   //  <=== End of "for theIdentifiedItem in fetchedResults
                print("Fetch Results first pass count = \(theRequiredDataMutableArray.count)")

                //Application prints the following:
                //ViewDidLoad came first.
                //January 1, 2021
                //February 1, 2021
                //Fetch Results first pass count = 2
            }
        } catch let error as NSError {
            print("Could not fetch. \(error), \(error.userInfo)")
        } //  <=== End of "do & catch"
        statusLabel.stringValue = ""
        tableView?.dataSource = self
        tableView?.delegate = self
        self.tableView?.reloadData()
    }   //  <=== End of "override func viewDidLoad()"

}   //  <=== End of "class TableViewDataBaseController: NSViewController

// MARK: - NSTableViewDataSource Extension

    extension MyTableViewViewController: NSTableViewDataSource {

        func numberOfRows(in tableView: NSTableView) -> Int {
            /// Show all the objects in the MutableArray.
            print("The theRequiredDataToShow data array = \(theRequiredDataMutableArray)")
            print("Fetch Results count again = \(theRequiredDataMutableArray.count)")

            //Application prints the following twice:

            //The theRequiredDataToShow data array = [<MyNumberDataRegistration: 0x6000017e5a40> (entity: MyNumberDataRegistration; id: 0xa153e0373b5c9c32
            //<x-coredata://8F69C401-0397-4543-8A63-A9165C8A7E39/MyNumberDataRegistration/p1>; data: {
            //calendarDate = "January 1, 2021";
            //}), <MyNumberDataRegistration: 0x6000017ffa70> (entity: MyNumberDataRegistration; id: 0xa8f502d324e08632 //<x-coredata://8F69C401-0397-4543-8A63-A9165C8A7E39/MyNumberDataRegistration/p2>; data: {
            //calendarDate = "February 1, 2021";
            //})]

            //Application prints the following once:
            //Fetch Results count again = 2
            //TableView Seque is active

            return theRequiredDataMutableArray.count
        }    //    <=== End of "func numberOfRows(in tableView: NSTableView) -> Int'
    }    //    <=== End of "extension NewTableViewController: NSTableViewDataSource"

// MARK: - NSTableViewDelegate Extension

    extension MyTableViewViewController: NSTableViewDelegate {

        func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
            if tableColumn?.identifier == NSUserInterfaceItemIdentifier(rawValue: "drawDateColumn") {
                let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "drawDateCell")
                guard let cellView = (tableView.makeView(withIdentifier:cellIdentifier, owner: self) as? NSTableCellView) else {return nil}

                //Application generates error message on next line: Value of type "NSManagedObject" has no member "calendarDate."

                cellView.textField?.stringValue = theRequiredDataMutableArray[row].calendarDate
                return cellView
            }
            return nil
        }    //    <=== End of "func tableView"
    }    //    <=== End of "extension NewTableViewController: NSTableViewDelegate"

change

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "MyNumberDataRegistration")

to

let fetchRequest = NSFetchRequest<MyNumberDataRegistration>(entityName: "MyNumberDataRegistration")

Hello deeje,

Thank you with your quick reply and suggestion ... :]

Unfortunately, the error "Value of type "NSManagedObject" has no member "calendarDate" still persists with the suggested change, complete with me deleting the original "Derived Data," cleaning, and rebuilding the application.

Again, thank you with your suggestion ...

jim_k

Core Data TableView Display Problem
 
 
Q