将NSDate插入CoreData时出错 [英] Error when inserting NSDate to CoreData
问题描述
NSDateFormatter *dt = [[NSDateFormatter alloc] init];
[dt setDateFormat:@"dd-MM-yyyy"];
NSDate *bdate = [dt dateFromString:@"02-02-2013"];
[nouContacte setValue:bdate forKey:@"birthday"];
我不明白为什么该代码不起作用。它会插入9个不同的数字,例如318366000。
我可以插入字符串,布尔值,数字,但我不能输入日期。
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.
coredata中的生日字段是日期类型。
birthday field in coredata is date type.
有什么想法吗?
推荐答案
日期属性,Core Data在SQLite文件中存储自2001年1月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]
例如, 2011-02-02 19:00:00 +0000
存储为数字 318366000
。
但是这是一个实现细节,您不必担心。
从SQLite文件中读取对象时,将数字转换回 NSDate
。
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
.
这篇关于将NSDate插入CoreData时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!