【问题标题】:Equivalent of SQL ORDER BY in NSPredicate - CoreDataNSPredicate 中的 SQL ORDER BY 等价物 - CoreData
【发布时间】:2016-02-01 05:54:15
【问题描述】:

NSPredicate 格式与以下 SQL 查询等效?

SELECT * FROM tableName ORDER BY date DESC LIMIT 1

我正在尝试这个

let predicate = NSPredicate(format: "ORDER BY date LIMIT 1")

但错误是“无法解析格式字符串”

【问题讨论】:

    标签: ios swift core-data nspredicate


    【解决方案1】:

    谓词用于过滤。这里没有过滤,不需要谓词。

    您希望对获取的结果进行排序,由NSFetchRequest 处理。

    按日期对结果排序,降序

    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
    

    限制您的结果

    fetchRequest.fetchLimit = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      相关资源
      最近更新 更多