基于NSDate升序对coreData排序不起作用

时间:2018-12-13 08:50:52

标签: swift sorting core-data nsdate

我尝试根据NSDate升序对true进行排序,但不起作用。

这是我的代码:

let activityPredicateCompound = NSCompoundPredicate.init(type: .and, subpredicates: [exampleIdKeyPredicate,userIdKeyPredicate])
let sortDescriptor = NSSortDescriptor(key: "nsDate", ascending: true)
fetchRequestActivity.sortDescriptors = [sortDescriptor]
fetchRequestActivity.fetchLimit = 10
fetchRequestActivity.predicate = activityPredicateCompound
let activities = try managedContext.fetch(fetchRequestActivity)

然后

for activity in activities {
print("activity nsDate:\(activity.value(forKey: "nsDate") as! NSDate)")}

打印结果为(真或假):

activity nsDate:2018-12-15 02:15:00 +0000
activity nsDate:2018-12-13 09:45:00 +0000

我想要这样的结果:

activity nsDate:2018-12-13 09:45:00 +0000
activity nsDate:2018-12-15 02:15:00 +0000

执行此操作的正确代码是什么?

0 个答案:

没有答案