Filter array of dictionaries by NSString

做~自己de王妃 提交于 2020-01-12 10:28:54

问题


while implementing search functionality I need to filter array of dictionaries. I am using auto complete textfield method for search bar and am storing it into string. I can able to parse the array,But facing with below 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"}]

Here I need to filter the dictionaries to make it finish

I tried with below code but its returning array with null values :(

 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]];

回答1:


This code will solve your problem it will return an array of dictionaries

NSString *substring = [NSString stringWithString:textField.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name contains[c] %@",searchString];
NSArray *filteredArry=[[arrayOfDict filteredArrayUsingPredicate:predicate] copy];

arrayOfDict is your original array of dictionaries

Swift 4.2 Version:::

let namePredicate = NSPredicate(format: "Name contains[c] %@",searchString)

let filteredArray = arrayOfDict.filter { namePredicate.evaluate(with: $0) }

print("names = \(filteredArray)")

Hope it will help you




回答2:


Here one observation is that filteredArrayUsingPredicate is a method of NSArray and you are using NSMutableArray instead.

Change NSMutableArray with temporary NSArray object for predicate.

For example:

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];



回答3:


You could use blocks instead. They are much less hand-wavy about match conditions.

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]];



回答4:


Hi I know there is many any answer, In my case I have stored values As JSON MODEL object, This is code for, using 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 is a NSMutableArray which contains Array of json model objects.If anyone need help regarding this, just ping me

Swift version above 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)");

More about this refer here



来源:https://stackoverflow.com/questions/28087465/filter-array-of-dictionaries-by-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!