32
-(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并且filteredArrayNSArrays。

hotelArray有类型的对象,hotel其中hotel有一个属性name

问题:我想hotelArray根据hotel.name何时hotel.name匹配在searchText[文本字段] 中输入的文本进行过滤,但我得到一个空的self.filteredArray.

4

5 回答 5

72

尝试以下几行,并确保properyName区分大小写。并且您已放置,谓词格式,这就是它不起作用的原因。只需用以下代码替换您的代码。

目标 C

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

迅速

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

使用迅捷filter

var searchText = "Galaxy"

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

斯威夫特 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)")

斯威夫特 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)")
于 2013-10-30T07:56:40.070 回答
5

根据您的信息,这是您的情况:

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

这个谓词应该适合你:

NSString *searchText = self.searchText.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name contains[c] %@", searchText];
self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:predicate];
于 2013-10-30T08:06:17.140 回答
1

这是可能对您有用的谓词方法。

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [filteredContactArray removeAllObjects];

    NSArray *tempArray = [hotelArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name contains[c] %@ OR name contains[cd] %@",searchText]];

    filteredArray = [NSMutableArray arrayWithArray:tempArray];//if you want the filtered array to be mutable or tempArray will work as as desired by you.
}

contains[c]- 表示区分大小写的谓词。contains[cd]- 不区分大小写的字符串

于 2015-12-16T05:59:07.507 回答
1

签出这个库

https://github.com/BadChoice/Collection

它带有许多简单的数组函数,再也不用写循环了

所以你可以这样做:

NSArray* hotels = [self.hotelArray filter:^BOOL(Hotel *hotel) {
    [return hotel.name isEqualToString:searchText];
}];

或者干脆

NSArray* hotels = [self.hotelArray where:@"name" is:searchText];

:)

于 2016-08-29T18:07:45.097 回答
0

我不确定这是否是您想要做的:

-(NSArray*)searchString:(NSString*)stringToSearch inArray:(NSArray*)myArray
{
    NSMutableArray* filtredArray = [[NSMutableArray alloc] init];
    for (NSString* elmnt in myArray)
    {
        if ([elmnt rangeOfString:stringToSearch].location != NSNotFound) [fitredArray addObject:elmnt];
    }
    return filtredArray;
}
于 2013-10-30T07:50:31.347 回答