ios 如何使用对象属性上的谓词过滤 NSArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19676371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:40:17  来源:igfitidea点击:

How to filter NSArray using predicate on an object property

iosiphoneobjective-c

提问by shifu

-(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;
}

hotelArrayand filteredArrayare NSArrays.

hotelArray并且filteredArrayNSArrays。

hotelArrayhas objects of type hotelwhere hotelhas a property name.

hotelArray具有类型为hotelwherehotel具有属性的对象name

Problem : I want to filter hotelArrayaccording to hotel.namewhen hotel.namematches text entered in searchText[text field], but I am getting an empty self.filteredArray.

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

回答by Dipen Panchasara

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

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

Objective C

目标 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

使用 swift filter

var searchText = "Galaxy"

let filteredArray = hotelArray.filter { 
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 { 
var filterByName = arrEmp.filter
do {
    
self.hotelArray      // Array in which we perform a search
self.filteredArray   // Result array
name                 // Property of the object used for the predicate
["name"] == "Mark" } print("filterByName filterByName)") var filterByAge = arrEmp.filter do {
NSString *searchText = self.searchText.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name contains[c] %@", searchText];
self.filteredArray = [self.hotelArray filteredArrayUsingPredicate:predicate];
["age"] as! Int > 25 } print("filterByAge filterByAge)")
["name"] == "Mark" } print("filterByName \(filterByName)") // *** Filter by Age *** var filterByAge = arrEmp.filter {
-(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.
}
["age"] as! Int > 25 } print("filterByAge \(filterByAge)")
["name"] == searchText } print("** Result ** \n\(filteredArray)")

Swift 3.0

斯威夫特 3.0

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

Swift 4.0

斯威夫特 4.0

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

回答by Marco Pace

Based on your information, this is your situation:

根据您的信息,您的情况如下:

-(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;
}

This predicate should work for you:

这个谓词应该适合你:

##代码##

回答by Devang Tandel

This is the predicate method that might work for you.

这是可能适合您的谓词方法。

##代码##

contains[c]- means the predicate with case sensitive. contains[cd]- case insensitive string

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

回答by Jordi Puigdellívol

Checkout this library

签出这个库

https://github.com/BadChoice/Collection

https://github.com/BadChoice/Collection

It comes with lots of easy array functions to never write a loop again

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

So you can just do:

所以你可以这样做:

##代码##

or simply

或者干脆

##代码##

:)

:)

回答by zbMax

I'm not sure if this is what you want to do :

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

##代码##