iOS CoreData NSPredicate to query multiple properties at once

后端 未结 8 1745
刺人心
刺人心 2020-12-23 22:39

I am trying to use a UISearchBar to query multiple properties of a NSManagedObject I have a NSManagedObject called Person

8条回答
  •  一生所求
    2020-12-23 23:05

    Complete solution for Swift2

    let request = NSFetchRequest(entityName: "Location")
    let subPredicate1 = NSPredicate(format: "(name = %@)", searchString)
    let subPredicate2 = NSPredicate(format: "(street = %@)", searchString)
    let subPredicate3 = NSPredicate(format: "(city = %@)", searchString)
    
    request.predicate = NSCompoundPredicate(type: .OrPredicateType, subpredicates: [subPredicate1, subPredicate2, subPredicate3])
    

提交回复
热议问题