我有一个使用表视图设置的核心数据数据库,我正在尝试将其设置为表视图中显示的对象按时间倒序显示的位置。我正在尝试使用 NSSortDescriptor 执行此操作,但无法弄清楚如何按时间倒序排序。有什么想法吗?谢谢。
这是我的代码:
func getCoreData(){
var appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context : NSManagedObjectContext = appDel.managedObjectContext!
var req : NSFetchRequest = NSFetchRequest(entityName: "KeptQuotes")
var s = NSSortDescriptor(key: "quote", ascending: true)
req.sortDescriptors = [s]
var error : NSError?
let fetchedResults = context.executeFetchRequest(req, error: &error) as [NSManagedObject]?
if let results = fetchedResults {
keptQuotes = results
}else{
println("Could not fetch (error), (error!.userInfo)")
}
tableView.reloadData()
}
在 NSDate 类型的实体中设置日期属性,然后将此属性设置为排序描述符并将升序设置为 false。
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]