I created the test application with Xcode Version 12.5 (12E262) on a Mac Mini (M1) as a Swift Core Data application.
I am not certain whether the group's documentation illustrates a solution. I apologize for not discovering a previous solution.
My code mimics the code in a previous question, located here: (https://developer.apple.com/forums/thread/123767), and answered by the group experts, so I thought I would try the same code to save the data, since the answer satisfied the author without any subsequent issues.
My test Core Data application version generates and stores multiple random "NULL" values, at the same time my application stores my singular data entry. The random incremental NULL values are separate entries, and appear before, or after my data input is saved. I check the input values with "DB Browser for SQLite," where the NULL entries are visible.
I would like to post the entire application code but the data entry limits me to a certain character count.
For the moment, I am at a loss, where I must thank you in advance for any suggestions as to whether my code is creating additional instances, or I misplaced a line of code. This is my first post, and if you decide to remark with a better way to post, please let me know.
Again thank you, and with my respect,
jim k
My save code is as follows:
I am not certain whether the group's documentation illustrates a solution. I apologize for not discovering a previous solution.
My code mimics the code in a previous question, located here: (https://developer.apple.com/forums/thread/123767), and answered by the group experts, so I thought I would try the same code to save the data, since the answer satisfied the author without any subsequent issues.
My test Core Data application version generates and stores multiple random "NULL" values, at the same time my application stores my singular data entry. The random incremental NULL values are separate entries, and appear before, or after my data input is saved. I check the input values with "DB Browser for SQLite," where the NULL entries are visible.
I would like to post the entire application code but the data entry limits me to a certain character count.
For the moment, I am at a loss, where I must thank you in advance for any suggestions as to whether my code is creating additional instances, or I misplaced a line of code. This is my first post, and if you decide to remark with a better way to post, please let me know.
Again thank you, and with my respect,
jim k
My save code is as follows:
Code Block import Cocoa import CoreData class MyAddNumbersController: NSViewController { var items: [NSManagedObject] required init? (coder aDecoder: NSCoder) { self.items = [] super.init(coder: aDecoder) } // <=== End of required init? (coder aDecoder: NSCoder) // Calendar and Number TextFields for the Attributes: ===> (All String Values) @IBOutlet weak var calendarDateTextField: NSTextField! @IBOutlet weak var firstNumberTextField: NSTextField! @IBOutlet weak var secondNumberTextField: NSTextField! @IBOutlet weak var thirdNumberTextField: NSTextField! @IBOutlet weak var fourthNumberTextField: NSTextField! @IBOutlet weak var fifthNumberTextField: NSTextField! @IBOutlet weak var sixthNumberTextField: NSTextField! @IBOutlet weak var bonusNumberTextField: NSTextField! // MARK: - View Did Load override func viewDidLoad() { super.viewDidLoad() if let calendarDateTextField = calendarDateTextField { calendarDateTextField.delegate = (self as NSTextFieldDelegate) } if let firstNumberTextField = firstNumberTextField { firstNumberTextField.delegate = (self as NSTextFieldDelegate) } if let secondNumberTextField = secondNumberTextField { secondNumberTextField.delegate = (self as NSTextFieldDelegate) } if let thirdNumberTextField = thirdNumberTextField { thirdNumberTextField.delegate = (self as NSTextFieldDelegate) } if let fourthNumberTextField = fourthNumberTextField { fourthNumberTextField.delegate = (self as NSTextFieldDelegate) } if let fifthNumberTextField = fifthNumberTextField { fifthNumberTextField.delegate = (self as NSTextFieldDelegate) } if let sixthNumberTextField = sixthNumberTextField { sixthNumberTextField.delegate = (self as NSTextFieldDelegate) } if let bonusNumberTextField = bonusNumberTextField { bonusNumberTextField.delegate = (self as NSTextFieldDelegate) } } // <=== End of override func viewDidLoad() // MARK: - Core Data Save Action @IBAction func saveNumberRegistration(_ sender: Any) { guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return } let managedObjectContext = appDelegate.persistentContainer.viewContext let entity = NSEntityDescription.entity(forEntityName: "MyNumberDataRegistration", in: managedObjectContext)! let registerApplicationNumberObject = NSEntityDescription.insertNewObject(forEntityName: "MyNumberDataRegistration", into: managedObjectContext) let component = NSManagedObject(entity: entity, insertInto: managedObjectContext) // The Entity Attributes. registerApplicationNumberObject.setValue(calendarDateTextField.stringValue, forKey: "calendarDate") registerApplicationNumberObject.setValue(firstNumberTextField.stringValue, forKey: "firstNumber") registerApplicationNumberObject.setValue(secondNumberTextField.stringValue, forKey: "secondNumber") registerApplicationNumberObject.setValue(thirdNumberTextField.stringValue, forKey: "thirdNumber") registerApplicationNumberObject.setValue(fourthNumberTextField.stringValue, forKey: "fourthNumber") registerApplicationNumberObject.setValue(fifthNumberTextField.stringValue, forKey: "fifthNumber") registerApplicationNumberObject.setValue(sixthNumberTextField.stringValue, forKey: "sixthNumber") registerApplicationNumberObject.setValue(bonusNumberTextField.stringValue, forKey: "bonusNumber") getCoreDataStoredObjectPath() do { try managedObjectContext.save() items.append (component) } catch { let nserror = error as NSError fatalError("There is an Unresolved Error : \(nserror), \(nserror.userInfo)") } // <=== End of do & catch } // <=== End of @IBAction func saveNumberRegistration(_ sender: Any) // MARK: - View Will Appear // Testing the Fetch Function. override func viewWillAppear() { super.viewWillAppear() guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return } let managedContext = appDelegate.persistentContainer.viewContext let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "MyNumberDataRegistration") do { items = try managedContext.fetch(fetchRequest) } catch let error as NSError { print("Could not fetch. \(error), \(error.userInfo)") } } // <=== End of override func viewWillAppear() // MARK: - Function to Locate and Identify the Container func getCoreDataStoredObjectPath() { let path = FileManager .default .urls(for: .applicationSupportDirectory, in: .userDomainMask) .last? .absoluteString .replacingOccurrences(of: "file://", with: "") .removingPercentEncoding print("This is the path to the Core Data storage location :: \n \n \(path ?? "Not found") \n ") } // <=== End of func getCoreDataStoredObjectPath() } // <=== End of class MyAddNumbersController: NSViewController // MARK: - Core Data TextField Extension extension MyAddNumbersController: NSTextFieldDelegate { func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool { if let textField = control as? NSTextField { print(textField.stringValue) } // <===End of if let textField = control as? NSTextField return true } // <=== End of func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) } // <=== End of extension MyAddNumbersController: NSTextFieldDelegate