Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter NSArray using predicate on an object property

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {   [self.map removeAnnotations:self.map.annotations];   if ([textField isEqual:self.searchText]) {       NSPredicate *bPredicate =        [NSPredicate predicateWithFormat:@"name contains[c],  %@",self.searchText.text];        self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:bPredicate];       NSLog(@" HEARE %@",self.filteredArray);       [self markAllHotels];   }   return YES; } 

hotelArray and filteredArray are NSArrays.

hotelArray has objects of type hotel where hotel has a property name.

Problem : I want to filter hotelArray according to hotel.name when hotel.name matches text entered in searchText [text field], but I am getting an empty self.filteredArray.

like image 355
shifu Avatar asked Oct 30 '13 07:10

shifu


People also ask

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

What is predicate in swift?

Predicates represent logical conditions, which you can use to filter collections of objects.

What is NSPredicate in Objective C?

CONTAINS operator : It allows to filter objects with matching subset. NSPredicate *filterByName = [NSPredicate predicateWithFormat:@"self. title CONTAINS[cd] %@",@"Tom"]; LIKE : Its simple comparison filter.


2 Answers

Try following lines, and make sure properyName is case sensitive. and you have placed , in predicate format, thats why its not working. just replace your code with following.

Objective C

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@",self.searchText.text]; self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:bPredicate]; NSLog(@"HERE %@",self.filteredArray); 

Swift

var bPredicate: NSPredicate = NSPredicate(format: "SELF.name contains[cd] %@", self.searchText.text) self.filteredArray = self.hotelArray.filteredArrayUsingPredicate(bPredicate) NSLog("HERE %@", self.filteredArray) 

Using swift filter

var searchText = "Galaxy"  let filteredArray = hotelArray.filter { $0["name"] == searchText } print("** Result ** \n\(filteredArray)") 

Swift 3.0

let arrEmp = [["name": "James", "age" : 27, "city" : "New york"],                    ["name": "Johnson", "age" : 24, "city" : "London"],                    ["name": "Alex", "age" : 28, "city" : "Newark"],                    ["name": "Mark", "age" : 25, "city" : "Paris"],                    ["name": "Steve", "age" : 25, "city" : "Silicon Valley"],                    ["name": "Lary", "age" : 28, "city" : "New york"]]  // *** Filter by Name exact match *** var filterByName = arrEmp.filter { $0["name"] == "Mark" } print("filterByName \(filterByName)")  // *** Filter by Age *** var filterByAge = arrEmp.filter { $0["age"] as! Int >  25 } print("filterByAge \(filterByAge)") 

Swift 4.0

var filterByName = arrEmp.filter do {     $0["name"] == "Mark" } print("filterByName filterByName)")  var filterByAge = arrEmp.filter do {     $0["age"] as! Int > 25 } print("filterByAge filterByAge)") 
like image 137
Dipen Panchasara Avatar answered Sep 21 '22 21:09

Dipen Panchasara


Based on your information, this is your situation:

self.hotelArray      // Array in which we perform a search self.filteredArray   // Result array name                 // Property of the object used for the predicate 

This predicate should work for you:

NSString *searchText = self.searchText.text; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name contains[c] %@", searchText]; self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:predicate]; 
like image 39
Marco Pace Avatar answered Sep 22 '22 21:09

Marco Pace