0
votes

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.

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.

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...

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.

I've come up with this so far, which is a mix from different tutorials I've found.

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) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    // returns the number of 'columns' to display.
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    // returns the # of rows in each component..
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> 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) -> 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)
        }
    }
}
1
So you have an NSManagedObject called dateOfEvent as part of MyEvents, right? You should be able to set the value to whatever the current date is with your datePicker by saying new event.dateOfEvent = datePicker.datePierce
Thanks, Pierce! That does save the date, however it also saves the time. And I want the date only :-)Polle
@Polle there is no "date only". Save actual dates in your database, and use date formatters to display those values to the user.jrturton
Aaaah, ok... So I'll have to save the entire date, including the time, and then simply exclude the time when I'm using the date? Thanks, man!Polle

1 Answers

0
votes

You should save dates as dates in your database. A date object represents a specific moment in time, therefore there is no "Date without time".

Use date formatters to only display the parts of the date that you are interested in to the user. Don't confuse display formatting with data storage.

So all you need is a Date type attribute on your managed object, which you set to the date property of the picker.