Yes, you need a second attribute declared as non-optional Date
for example dateCreated
. In the NSManagedObject
subclass override awakeFromInsert
to assign the current date when the record is created.
override func awakeFromInsert() {
super.awakeFromInsert()
dateCreated = Date()
}
Then you can sort
request.sortDescriptors = [NSSortDescriptor(key: "dateCreated", ascending: true)]
in ascending
order means the oldest entry comes first.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…