title: Save date to core data
body: <p>Ok, so I'm trying to learn Swift 3 in XCode 8. And I've followed some tutorials, and I'm trying to build my own app that'll let me save an event with the date and a category.</p>
  
  <p>Right now I'm having a hard time getting to save the date to the core data. On top of that, I would like to display the full datestyle in the UITextfield, but save it as short datestyle in the core data.</p>
  
  <p>Does anyone have a solution for this? And please note that I'm a n00b, I'm not in to all of the correct lingo. So please show me what I need to change, as I am still learning what all the different things are...</p>
  
  <p>What I think I should do, is to make a function that'll format the string to save with the core data (attribute 'dateOfEvent' of entity 'MyEvents' is set to 'date'), and then call it from the datePickerChanged function to keep it updated at all times.</p>
  
  <p>I've come up with this so far, which is a mix from different tutorials I've found.</p>
  
  <pre><code>import UIKit
  
  class addEventViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
      @IBOutlet weak var addEventNameInput: UITextField!
      @IBOutlet weak var addEventDateInput: UITextField!
      @IBOutlet weak var addEventCategoryInput: UITextField!
  
      var addEventCategories = ["Født", "Startede børnehave", "Startede uddannelse", "Startede job", "Flyttede hjemmefra", "Startede romantisk forhold", "Startede forhold"]
      var categoryPicker = UIPickerView()
  
      let datePicker = UIDatePicker()
  
      func textFieldDidBeginEditing(_ textField: UITextField) {
          addEventDateInput.inputView = datePicker
          datePicker.datePickerMode = .date
          datePicker.addTarget(self, action: #selector(datePickerChanged), for: .valueChanged)
      }
  
      func datePickerChanged(sender: UIDatePicker) {
          let formatter = DateFormatter()
          formatter.dateStyle = .full
          formatter.timeStyle = .none
  
          addEventDateInput.text = formatter.string(from: sender.date)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          addEventNameInput.delegate = self
          addEventDateInput.delegate = self
  
          categoryPicker.delegate = self
          categoryPicker.dataSource = self
          addEventCategoryInput.inputView = categoryPicker
      }
  
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          textField.resignFirstResponder()
          return true
      }
  
      // returns the number of 'columns' to display.
      func numberOfComponents(in pickerView: UIPickerView) -&gt; Int {
          return 1
      }
  
      // returns the # of rows in each component..
      func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
          return addEventCategories.count
      }
  
      func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          addEventCategoryInput.text = addEventCategories[row]
      }
  
      func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {
          return addEventCategories[row]
      }
  
      @IBAction func addEventButtonPressed(_ sender: Any) {
          if addEventNameInput.text == "" {
          } else {
              let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  
              let newevent = MyEvents(context: context)
              newevent.nameOfevent = addEventNameInput.text!
              newevent.categoryOfevent = addEventCategoryInput.text!
  
              // Save data to coredata
              (UIApplication.shared.delegate as! AppDelegate).saveContext()
  
              // Pop back to events list
              navigationController!.popViewController(animated: true)
          }
      }
  }
  </code></pre>