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
How to filter NSArray using predicate on an object property
提问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;
}
hotelArray
and filteredArray
are NSArray
s.
hotelArray
并且filteredArray
是NSArray
s。
hotelArray
has objects of type hotel
where hotel
has a property name
.
hotelArray
具有类型为hotel
wherehotel
具有属性的对象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
.
问题:我想hotelArray
根据hotel.name
何时hotel.name
匹配在searchText
[文本字段] 中输入的文本进行过滤,但我得到一个空的self.filteredArray
.
回答by Dipen Panchasara
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.
尝试以下几行,并确保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 :
我不确定这是否是您想要做的:
##代码##