-(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 NSArray
s.
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
.
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.
Predicates represent logical conditions, which you can use to filter collections of objects.
CONTAINS operator : It allows to filter objects with matching subset. NSPredicate *filterByName = [NSPredicate predicateWithFormat:@"self. title CONTAINS[cd] %@",@"Tom"]; LIKE : Its simple comparison filter.
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)")
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With