Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...