What is the right way to insert or write today's date in Core Data?
in .xcdatamodeld I have the Entity Contingut and the Attributes:
dateCreated of Type Date
text1 of Type String
func today1() {
let date = Date()
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
let month = calendar.component(.month, from: date)
let year = calendar.component(.year, from: date)
let avui = "\(year)-\(month)-\(day)"
print (avui)
}
// CREATE
@IBAction func createData(_ sender: UIButton) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let contingutEntity = NSEntityDescription.entity(forEntityName: "Contingut", in: managedContext)!
let contingut = NSManagedObject(entity: contingutEntity, insertInto: managedContext)
contingut.setValue(today1(), forKey: "dateCreated")
contingut.setValue("some text", forKey: "text1")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
When I create a new text1, I want to register the day. What is the right way to do that?