ios - 按日期和字符对核心数据属性进行排序

标签 ios iphone swift xcode core-data

我正在尝试搜索我的核心数据,默认情况下它会搜索标题属性,但我还需要按日期对结果进行排序。

我的代码搜索 title 属性,但如何创建两个 谓词 并将它们与 context 连接?

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let currentText = textField.text ?? ""
    let prospectiveText = (currentText as NSString).stringByReplacingCharactersInRange(range, withString: string)

    //Load data from Core Data
    let appDel : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    context =  appDel.managedObjectContext

    do {

        request =  NSFetchRequest(entityName: "Event")
        let sort:NSSortDescriptor = NSSortDescriptor(key:"title", ascending: true)
        request.sortDescriptors = [sort]
        let searchPredicate = NSPredicate(format: "title CONTAINS[c] %@", prospectiveText)
        request.predicate = searchPredicate
        results = try context.executeFetchRequest(request)

        animateTableCell()


    } catch {

        print("ERROR")
    }

    return true;
}

最佳答案

尚不清楚您是否对选择排序感兴趣。谓词用于确定选择哪些记录,排序描述符用于对结果进行排序。

对于排序,request.sortDescriptors 是一个描述符数组。所以你可以有多个描述符,例如:

let sort:NSSortDescriptor = NSSortDescriptor(key:"title", ascending: true)
let sort2:NSSortDescriptor = NSSortDescriptor(key:"date", ascending: false)
request.sortDescriptors = [sort, sort2]

应按标题升序排序,然后按日期降序排序(即具有相同标题的项目应按日期降序排列)。

关于ios - 按日期和字符对核心数据属性进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512305/

相关文章:

ios - 重复符号错误 - Google 登录和 Google Analytics 两者

ios - 使用 WebKit 加载 YouTube 视频时控制台中打印警告

ios - 在 Swift 3 中处理 try 和 throws

ios - iPhone 静态数据调用

android - 如何将 "share"选项添加到 iOS/iPhone 或 Android 图片库?

ios - 在 CollectionView 上滑动删除

iphone - 在输入文本字段之前禁用按钮?

Swift:一个函数作为另一个函数的参数

ios - 是否可以判断用户何时更新应用程序?

ios - xmpp 书签 xep-0048 -- 服务器未添加书签