Core Data Error with NSDate

hello:

Can someone tell me why I am getting a crash when saving a date attribute in Core Data:


Unacceptable type of value for attribute: property = "date"; desired type = NSDate; given type = NSDate; value = NSDate.


let date = NSDate()
. . .
let groupObject = NSEntityDescription.insertNewObject(forEntityName: "GroupRecord", into: managedContext)

groupObject.setValue(NSDate.self, forKeyPath: "date")// crash occurs here
do {
try managedContext.save()
Answered by OOPer in 400263022

`NSDate.self` represents a type object, which is not an instance of `NSDate`.

Try something like this:

groupObject.setValue(Date(), forKeyPath: "date")// crash occurs here
Accepted Answer

`NSDate.self` represents a type object, which is not an instance of `NSDate`.

Try something like this:

groupObject.setValue(Date(), forKeyPath: "date")// crash occurs here

Hello OOPer:


Your suggestion implemented got rid of the error. However, the database record had the following in date column:

(timestamp) 599448923.068390

How can I get the date format dd/mm/yyyy


I am using the system date.

Please explain what you mean by `date column`.

That is the attribute for "date" in Core Data store. I am using DB Browser to read the data in Core Data store where every attribute data is stored in a column.

That's a problem of DB Browser, which I have never used. I guess that tool is showing the internal representation of `NSDate`/`Date`.

Maybe you can find some settings for showing timestamps or dates.


If you want to know how to display the `date` column as `dd/mm/yyyy` in your app, I (or some other reader) can be some help.

OK.


I read where the number in Core Data is the date in nanoseconds which a converter can show in regular date.


Thanks

Core Data Error with NSDate
 
 
Q