3

我正在尝试使用谓词从 NSMutableArray 中获取 NSDictionary:

NSMutableArray *arr = [NSMutableArray arrayWithArray:[self createArrayFromCSV:savedCSV withDelimiter:delim firstLineContainsKeys:YES]];

// The method «createArrayFromCSV returns an array of dictionaries

NSLog(@"Array %@", [arr objectAtIndex:0]);  // works fine

// spits out: 

// Array {
//     product = "MyProduct";
//     "id_product" = 29;
// }

// idField and idVal are passed to the method

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ == %@)", idField, idVal];

NSLog(@"predicate %@", predicate);

// spits out predicate "id_product" == "29"

NSArray *filtered = [arr filteredArrayUsingPredicate:predicate];

NSLog(@"Filtered %@", filtered);

// Filtered is always empty

任何想法我做错了什么???

[已解决] 显然,我把格式字符串弄错了。这有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)", idField, idVal];

根据这篇文章NSPredicate predicateWithFormat 传入属性名称

对于键或键路径,必须使用 @K

4

2 回答 2

8

显然,我把格式字符串弄错了。这有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)", idField, idVal];

根据这篇文章 NSPredicate predicateWithFormat 传入属性名称

对于键或键路径,必须使用 @K

于 2013-05-25T21:40:32.703 回答
1

这对我有用:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email contains[c] %@",searchString];

filteredArray = [[NSMutableArray alloc] initWithArray: [[arrayData filteredArrayUsingPredicate:predicate] copy]];
于 2016-09-30T07:40:46.853 回答