2

我有一个数组

NSArray *arry = [[NSArray alloc]initWithObjects:@"1322",@"13222",@"13322",@"14322",@"13522",@"21322",@"11322",@"13212" ,nil];

我想得到有对象的数组

@"1322",@"13222",@"13322",@"13212"

你能告诉我如何为这个过滤器使用谓词吗?

4

4 回答 4

3

我不确定我是否正确理解了您的问题(和评论)。

要查找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
)
于 2013-07-02T08:54:41.973 回答
1

尝试使用这个

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];
于 2013-07-02T08:37:44.187 回答
0
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将包含您过滤的数据。我认为它也会起作用。

于 2013-08-02T14:02:36.803 回答
0
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

于 2013-07-02T08:38:04.500 回答