我有一个数组
NSArray *arry = [[NSArray alloc]initWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212" ,nil];
我想得到有对象的数组
@"1322",@"13222",@"13322",@"13212"
你能告诉我如何为这个过滤器使用谓词吗?
我有一个数组
NSArray *arry = [[NSArray alloc]initWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212" ,nil];
我想得到有对象的数组
@"1322",@"13222",@"13322",@"13212"
你能告诉我如何为这个过滤器使用谓词吗?
我不确定我是否正确理解了您的问题(和评论)。
要查找array
包含在另一个数组中的所有对象wanted
,您可以使用“SELF IN %@”谓词:
NSArray *array = [[NSArray alloc] initWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212" ,nil];
NSArray *wanted = [[NSArray alloc] initWithObjects:@"1322", @"13522", @"999", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", wanted];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filtered);
输出:
( 1322, 13522 )
尝试使用这个
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like [c]'1*1*1'"];
NSArray *arry = [NSArray arrayWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212", nil];
NSArray *resltArry = [array filteredArrayUsingPredicate:predicate];
NSArray *yourArray = [[NSArray alloc]initWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212" ,nil];
NSArray *filter = [[NSArray alloc] initWithObjects:@"1322", @"13522", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", filter];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
这filteredArray
将包含您过滤的数据。我认为它也会起作用。
NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF like [c]'13***'"];
NSArray *array = [NSArray arrayWithObjects:@"11321", @"13121", @"23121", @"1111", nil];
NSArray *resultArray = [array filteredArrayUsingPredicate:p];
NSLog(@"%@",resultArray);
o/p
( 13121,
)
你也可以参考这个链接 thisone