问题
NSDateFormatter *dt = [[NSDateFormatter alloc] init];
[dt setDateFormat:@"dd-MM-yyyy"];
NSDate *bdate = [dt dateFromString:@"02-02-2013"];
[nouContacte setValue:bdate forKey:@"birthday"];
I don't understand why that code doesn't work. It inserts 9 different digits like 318366000. I can insert strings, "booleans", numbers, but i can't with date.
birthday field in coredata is date type.
any idea?
回答1:
For a "Date" attribute, Core Data stores the "number of seconds since 1 January 2001, GMT" in the SQLite file, i.e. what you get with
[bdate timeIntervalSinceReferenceDate]
For example, 2011-02-02 19:00:00 +0000 is stored as the number 318366000.
But that is an implementation detail and you should not worry about it.
When reading objects from the SQLite file, the number is converted back do an NSDate.
来源:https://stackoverflow.com/questions/21516696/error-when-inserting-nsdate-to-coredata