sam_852
sam_852

Reputation: 11

Search multi words in a single search iOS swift using NSPredicate

I want to search a list by typing multi words from the detailed list. e.g, in a claim request list there are different type of lables such as amount, requestor name, request name and nos also. So i want to search anything from this label so that i can able to find the exact request.

In this image i have made a sample list having type, requestor, reg no and amount. I want to search the list by typing multi words using these params.

Upvotes: 1

Views: 504

Answers (2)

B.Saravana Kumar
B.Saravana Kumar

Reputation: 1242

May be this answer was helpful to you..

let addresspredicate = NSPredicate(format: "address_name contains[c] %@",searchText)
    let accnopredicate = NSPredicate(format: "acc_no contains[c] %@",searchText)
    let propertytype = NSPredicate(format: "property_type contains[c] %@",searchText)
    let subpropertytypoe = NSPredicate(format: "subproperty_type contains[c] %@",searchText)

    let predicateCompound = NSCompoundPredicate.init(type: .or, subpredicates: [addresspredicate,accnopredicate,propertytype,subpropertytypoe])
    filteredProperty = (propertyArray as Array).filter { predicateCompound.evaluate(with: $0) };
    print("filteredProperty = ,\(filteredProperty)")

Upvotes: 0

Sreejith S
Sreejith S

Reputation: 376

var predicateList = [NSPredicate]()

let words = filterText.componentsSeparatedByString(" ")

for word in words{

 if count(word)==0{
       continue
 }

 let RequestTypeArray = NSPredicate(format: "RequestType contains[c] %@", word)
 let RequestEmployeeArray = NSPredicate(format: "RequestorEmployee contains[c] %@", word)
 let RegesterNumberArray = NSPredicate(format: "ReqNo contains[c] %@", word)
 let AmountOrDaysArray = NSPredicate(format: "AmountOrDays contains[c] %@", word)

 let orCompoundPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [firstNamePredicate, lastNamePredicate,departmentPredicate,jobTitlePredicate])

 predicateList.append(orCompoundPredicate)
}

  request.predicate = NSCompoundPredicate(type:    NSCompoundPredicateType.AndPredicateType, subpredicates: predicateList)

Upvotes: 1

Related Questions