Core Data Query Multiple Columns with Single Search String

后端 未结 2 1042
长发绾君心
长发绾君心 2021-01-22 11:21

I have a simple Core Data model with two string attributes (size and category). Given a search string like \'small widget\' is it possible to return records that match all the q

2条回答
  •  感情败类
    2021-01-22 11:45

    For Swift you can fetch multiple columns by using propertiesToFetch

    let fetchRequest = NSFetchRequest(entityName: "")
    let predicate = NSPredicate(format: "")
    fetchRequest.predicate = predicate
    fetchRequest.resultType = .dictionaryResultType
    
    
    // Here is where you can query multiple columns, you can add as much columns as you want and fetch them as an array of dictionaries
    fetchRequest.propertiesToFetch = ["column1", "column2", "column3, "column4"]
    
    
    let sort = NSSortDescriptor(key: "column1", ascending: false)
    fetchRequest.sortDescriptors = [sort]
    

提交回复
热议问题