【问题标题】:How to sort and limit with NSPredicate in CoreData? [duplicate]如何在 CoreData 中使用 NSPredicate 进行排序和限制? [复制]
【发布时间】:2017-10-22 22:24:12
【问题描述】:

我想获取保存在 Core Data 中并按名为“recordingTime”的Date 类型属性排序的五条记录。 例如,在 MySql 中,我们可以简单地添加limit = 5 来设置限制。 那么,如何使用 NSPredicate 进行排序和限制呢? 谢谢。

【问题讨论】:

    标签: ios objective-c swift core-data nspredicate


    【解决方案1】:

    您可以使用NSFetchRequestsortDescriptorsfetchLimit 属性。

    目标 C:

    //Set sort
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"recordingTime" ascending:YES];
    [request setSortDescriptors: @[sort]];
    [request setFetchLimit:5];
    

    斯威夫特:

    let sort = NSSortDescriptor(key: "recordingTime", ascending: true)
    request.sortDescriptors = [sort]
    request.fetchLimit = 5
    

    【讨论】:

    • @CallOfOrange 欢迎朋友 :)
    猜你喜欢
    • 2014-10-21
    • 2019-01-31
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多