ios - NSPredicate:获取最高值

标签 ios swift nspredicate nsfetchrequest nssortdescriptor

我有以下内容:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjContext = appDelegate.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "RAS")
    var error: NSError?
    let predicate1 = NSPredicate(format: "%K AND %K == false",  "rasScore", "rasStatus")

    fetchRequest.predicate = predicate1

    do {...

这为我提供了符合 NSPredicate 参数的所有记录,但我只想要 rasScore 中具有最高值的记录。我该怎么做才能得到它?是否可以将其包含在 NSPredicate 格式中?

最佳答案

fetchRequestfetchLimit 设置为 1,并使用描述符按 "rasScore" 的值降序排序,如下所示:

fetchRequest.fetchLimit = 1
var highestSortDescriptor : NSSortDescriptor = NSSortDescriptor(key: "rasScore", ascending: false)
fetchRequest.sortDescriptors = [highestSortDescriptor]

关于ios - NSPredicate:获取最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061029/

相关文章:

ios - 仅获取共享特定属性值的核心数据对象

ios - Storyboard中的横向和纵向方向

ios - 如何为本地通知接口(interface)编写适当的测试?

ios - 导航项不更改标题

core-data - 核心数据 : Extend Predicate to fetch related entities with special Attribute-Value

iphone - NSPredicate同时使用IN和LIKE

ios - 如何在 iOS swift 中滑动 Collection View 时调用 map 注释?

ios - 如何在 UITextView、Swift3 和 Parse 服务器上显示数组元素 (bitnami)

ios - UIProgressView 设置进度

ios - NSPredicate 带有 !=?