Hello:
I bult an app for iOS successfully using CoreData. Now I am working on an app on Mac OSx with the latest version of Swift and Xcode.
However, I am unable to save any data. I am relatively new to Xcode so my problem, I'm sure, is due to a lack of expertise with programming in Swift. (I have done a lot of work with Oracle).
I am currently trying to save information entered into text fields to CoreData, without success.
Below is my code. I get no runtime errors. I hope someone can point me to the changes I need to make.
- import Cocoa
- import CoreData
- class Register: NSViewController {
- var items: [NSManagedObject]
- requiredinit?(coder aDecoder: NSCoder) {
- self.items = []
- super.init(coder: aDecoder)
- }
- overridefunc viewDidLoad() {
- super.viewDidLoad()
- if let firstNameTextField = firstNameTextField {
- firstNameTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let middleNameTextField = middleNameTextField {
- middleNameTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let lastNameTextField = lastNameTextField {
- lastNameTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let birthdateTextField = birthdateTextField {
- birthdateTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let schoolTextField = schoolTextField {
- schoolTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let schoolAddressTextField = schoolAddressTextField {
- schoolAddressTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let schoolCityTextField = schoolCityTextField {
- schoolCityTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let schoolStateTextField = schoolStateTextField {
- schoolStateTextField.delegate = (self as NSTextFieldDelegate)
- }
- if let emailTextField = emailTextField {
- emailTextField.delegate = (self as NSTextFieldDelegate)
- }
- }
- @IBOutletweakvar firstNameTextField: NSTextField!
- @IBOutletweakvar middleNameTextField: NSTextField!
- @IBOutletweakvar lastNameTextField: NSTextField!
- @IBOutletweakvar birthdateTextField: NSTextField!
- @IBOutletweakvar schoolTextField: NSTextField!
- @IBOutletweakvar schoolAddressTextField: NSTextField!
- @IBOutletweakvar schoolCityTextField: NSTextField!
- @IBOutletweakvar schoolStateTextField: NSTextField!
- @IBOutletweakvar emailTextField: NSTextField!
- @IBActionfunc saveRegistrant(_ sender: Any) {
- print("SAVE ACTION STARTED")
- let appDelegate = NSApplication.shared.delegate as! AppDelegate
- let managedContext = appDelegate.persistentContainer.viewContext
- let entity = NSEntityDescription.entity(forEntityName: "Registration", in: managedContext)!
- let registerStudentsObject = NSEntityDescription.insertNewObject(forEntityName: "Registration", into: managedContext)
- print("MOC READ")
- let component = NSManagedObject(entity: entity, insertInto: managedContext)
- registerStudentsObject.setValue(firstNameTextField, forKeyPath: "firstName")
- registerStudentsObject.setValue(middleNameTextField, forKey: "middleName")
- registerStudentsObject.setValue(lastNameTextField, forKey: "lastName")
- registerStudentsObject.setValue(birthdateTextField, forKey: "dob")
- registerStudentsObject.setValue(schoolTextField, forKey: "school")
- registerStudentsObject.setValue(schoolAddressTextField, forKey: "schoolAddress")
- registerStudentsObject.setValue(schoolCityTextField, forKey: "schoolCity")
- registerStudentsObject.setValue(schoolStateTextField, forKey: "schoolState")
- registerStudentsObject.setValue(emailTextField, forKey: "email")
- print("FIELDS READ")
- do
- {
- try managedContext.save()
- items.append (component)
- print("saved")
- }
- catch
- {
- }
- }
- }
- extension Register: NSTextFieldDelegate {
- func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
- if let textField = control as? NSTextField {
- print(textField.stringValue)
- }
- return true
- }
- }