在实现搜索功能时,我需要过滤字典数组。我正在使用自动完整的文本字段方法搜索栏并将其存储到字符串中。我能够解析数组,但面对json
[{"CertProfID":"4","Name":"Dodge","Location":"loc4","City":"city4","State":"state4","Zip":"zip5","Website":"http:\/\/cnn.com","Phone":"phone4","Email":"email4"},
{"CertProfID":"5","Name":"cat","Location":"loc5","City":"city5","State":"State5","Zip":"zip5","Website":"web5","Phone":"phone5","Email":"email5"}]
在这里,我需要过滤字典以使其完成
我尝试使用下面的代码但是它的返回数组为空值:(
NSString *substring = [NSString stringWithString:textField.text];
NSLog(@"substring %@",substring);
NSMutableArray *arr2Filt= [arraylist valueForKey:@"Name"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",substring];
filteredarr = [NSMutableArray arrayWithArray:[arr2Filt filteredArrayUsingPredicate:predicate]];
答案 0 :(得分:8)
此代码将解决您的问题,它将返回一个字典数组
NSString *substring = [NSString stringWithString:textField.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name contains[c] %@",searchString];
NSArray *filteredArry=[[arrayOfDict filteredArrayUsingPredicate:predicate] copy];
arrayOfDict是您原始的词典数组
Swift 4.2版本 :::
let namePredicate = NSPredicate(format: "Name contains[c] %@",searchString)
let filteredArray = arrayOfDict.filter { namePredicate.evaluate(with: $0) }
print("names = \(filteredArray)")
希望它能帮到你
答案 1 :(得分:2)
这里有一个观察结果是filteredArrayUsingPredicate
是NSArray
的一种方法,而您正在使用NSMutableArray
。
使用谓词的临时NSMutableArray
对象更改NSArray
。
例如:
NSString *substring = [NSString stringWithString:textField.text];
NSArray *tempArray = [arraylist valueForKey:@"Name"];
// If [arraylist valueForKey:@"Name"]; line returns NSMutableArray than use below line
// NSArray *tempArray = [NSArray arrayWithArray:[arraylist valueForKey:@"Name"]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",substring];
filteredarr = [[tempArray filteredArrayUsingPredicate:predicate] mutableCopy];
答案 2 :(得分:2)
您可以使用块代替。在比赛条件方面,他们不那么手艺了。
NSString *substring = [NSString stringWithString:textField.text];
NSLog(@"substring %@",substring);
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject[@"Name"] containsString:substring];
}];
filteredarr = [NSMutableArray arrayWithArray:[arraylist filteredArrayUsingPredicate:predicate]];
答案 3 :(得分:1)
嗨,我知道有很多答案, 在我的情况下,我将值存储为 JSON MODEL 对象,这是使用 JSON MODEL
的代码 NSString *searchString = searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDictionary *temp=[evaluatedObject toDictionary];
return [temp[@"user"][@"firstName"] localizedCaseInsensitiveContainsString:searchString];
}];
filteredContentList = [NSMutableArray arrayWithArray:[searchListValue filteredArrayUsingPredicate:predicate]];
[tableViews reloadData];
searchListValue 是 NSMutableArray ,其中包含json模型对象数组。如果有人需要帮助,请点击我
Swift版本高于2.2
var customerNameDict = ["firstName":"karthi","LastName":"alagu","MiddleName":"prabhu"];
var clientNameDict = ["firstName":"Selva","LastName":"kumar","MiddleName":"m"];
var employeeNameDict = ["firstName":"karthi","LastName":"prabhu","MiddleName":"kp"];
var attributeValue = "ka";
var arrNames:Array = [customerNameDict,clientNameDict,employeeNameDict];
//var namePredicate =
// NSPredicate(format: "firstName like %@",attributeValue);
//uncomment above line to search particular word
let namePredicate =
NSPredicate(format: "firstName contains[c] %@",attributeValue);
let filteredArray = arrNames.filter { namePredicate.evaluateWithObject($0) };
print("names = ,\(filteredArray)");
有关此refer here
的更多信息