使用NSPredicate在单个搜索iOS swift中搜索多个单词

时间:2017-03-28 04:30:25

标签: ios arrays swift3 nspredicate

我想通过从详细列表中键入多个单词来搜索列表。 例如, 在索赔请求列表中,有不同类型的标签,例如金额,请求者名称,请求名称和nos。所以我想从这个标签中搜索任何内容,以便我能够找到确切的请求。

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.

2 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:0)

可能这个答案对你有帮助..

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)")