Objective c 使用NSPredicate筛选NSDictionary的NSMutableArray,以查找;“关键”;存在

Objective c 使用NSPredicate筛选NSDictionary的NSMutableArray,以查找;“关键”;存在,objective-c,nsdictionary,nspredicate,Objective C,Nsdictionary,Nspredicate,我有一系列字典。我正在使用NSPredicate对数组进行筛选,以查找是否存在具有特定键的字典 我提到了各种各样的例子,其中一个最接近的例子是:。但是,我不希望该键有值。我的问题是我想先找到钥匙。我尝试了不同的语法,但没用 到目前为止我所做的: NSString *key = @"open_house_updated_endhour"; NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K", k

我有一系列字典。我正在使用NSPredicate对数组进行筛选,以查找是否存在具有特定键的字典

我提到了各种各样的例子,其中一个最接近的例子是:。但是,我不希望该键有值。我的问题是我想先找到钥匙。我尝试了不同的语法,但没用

到目前为止我所做的:

    NSString *key = @"open_house_updated_endhour";
    NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K", key];
    //NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains [cd]", key]; Doesn't work.
    //NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", key]; Won't work because it expects a value here.
    NSLog(@"predicate %@",predicateString);
    NSArray *filtered = [updatedDateAndTime filteredArrayUsingPredicate:predicate]; // updatedDateAndTime is the NSMutableArray
试试看:

 NSArray *Myarray = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"my hello string" forKey:@"name"]];   
NSArray *filteredarray = [Myarray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(name == %@)", @"my hello string"]];
NSLog("%@",filteredarray);
另一个例子:

NSString *mycategory = @"iamsomeone";
NSArray *myitems = @[@{ @"types" : @[@"novel", @"iamsomeone", @"dog"] },
                   @{ @"types" : @[@"cow", @"iamsomeone-iam", @"dog"] },
                   @{ @"types" : @[@"cow", @"bow", @"cat"] }];
NSPredicate *mypredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    NSArray *categories = [evaluatedObject objectForKey:@"types"];
    return [categories containsObject:mycategory];
}];

NSArray *outpuArray = [myitems filteredArrayUsingPredicate:mypredicate];
NSLog(@"hello output:%@",outpuArray);
试试看:

 NSArray *Myarray = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"my hello string" forKey:@"name"]];   
NSArray *filteredarray = [Myarray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(name == %@)", @"my hello string"]];
NSLog("%@",filteredarray);
另一个例子:

NSString *mycategory = @"iamsomeone";
NSArray *myitems = @[@{ @"types" : @[@"novel", @"iamsomeone", @"dog"] },
                   @{ @"types" : @[@"cow", @"iamsomeone-iam", @"dog"] },
                   @{ @"types" : @[@"cow", @"bow", @"cat"] }];
NSPredicate *mypredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    NSArray *categories = [evaluatedObject objectForKey:@"types"];
    return [categories containsObject:mycategory];
}];

NSArray *outpuArray = [myitems filteredArrayUsingPredicate:mypredicate];
NSLog(@"hello output:%@",outpuArray);

字典将
nil
作为缺少键的值传递。因此,只需将键与
nil
进行比较即可

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K==NULL", key]; // Dictionaries not having the key
NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K!=NULL", key]; // Dictionaries having the key

字典将
nil
作为缺少键的值传递。因此,只需将键与
nil
进行比较即可

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K==NULL", key]; // Dictionaries not having the key
NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K!=NULL", key]; // Dictionaries having the key

所以你只想知道数组中包含的字典中是否存在“某个键”?@AdeelMiraj是的,没错!这就是我想要的。在我看来,枚举数组相对来说更简单、更直接。通过谓词这样做有什么特别的原因吗?@AdeelMiraj。。。之所以这样做,是因为我在表视图中为各种单元格提供了冗余逻辑。因此,我不想增加不必要的代码逻辑。另外,由于我是iOS新手,我想使用可用的NSPredicate系统来实现这一目标。所以你只想知道数组中包含的字典中是否存在“某个键”?@AdeelMiraj是的,没错!这就是我想要的。在我看来,枚举数组相对来说更简单、更直接。通过谓词这样做有什么特别的原因吗?@AdeelMiraj。。。之所以这样做,是因为我在表视图中为各种单元格提供了冗余逻辑。因此,我不想增加不必要的代码逻辑。另外,由于我是iOS新手,我想使用可用的NSPredicate系统来实现这一目标。感谢您的快速响应。但是,第一个示例希望我对键有一个值。就我而言,它可能在那里,也可能不在那里。因此,我想先检查一下钥匙。谢谢您的快速回复。但是,第一个示例希望我对键有一个值。就我而言,它可能在那里,也可能不在那里。因此,我想先检查一下钥匙。非常感谢您的快速帮助。这有帮助。我得到一个包含所需dictionary对象(如果存在)的数组。如果我想使用@“%K==NULL”| |@“%K!=NULL”怎么办?如果我这样做,我会得到一个错误。基本上,如果找到密钥,我会更新字典。不管它是空的还是空的。毫无疑问,我喜欢这个简单的解决方案。我不明白,为什么要使用
@“%K==NULL”|124;@“%K!=NULL”
。首先,在第一个
NULL
之后还有一个额外的
,什么是错误。你能告诉我你得到的错误吗?但是,这个表达式永远都是真的。这毫无意义。非常感谢你的快速帮助。这很有帮助。我得到了一个包含所需字典对象的数组,如果它存在的话。如果我想使用@“%K==NULL”|怎么办%K!=NULL“”?如果我这样做,我会得到一个错误。基本上,如果找到密钥,我会更新字典。不管它是否为空。毫无疑问,我喜欢这个简单的解决方案。我不明白,为什么要使用
@“%K==NULL”|@“%K!”空“
。首先,在第一个
NULL
之后有一个额外的
,这是一个错误。你能告诉我你的错误吗?然而,这个表达永远是正确的。这毫无意义。