【发布时间】:2017-02-20 09:38:47
【问题描述】:
我有一个 NSMutableArray 的字典。我正在使用 NSPredicate 过滤数组以查找具有特定键的字典是否存在。
我提到了各种例子,最接近的例子之一在这里:Using NSPredicate to filter an NSArray based on NSDictionary keys。但是,我不希望密钥具有价值。我的问题是我想先找到钥匙。我尝试了不同的语法,但没有帮助。
到目前为止我做了什么:
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
【问题讨论】:
-
所以您只想知道数组中包含的字典中是否存在“某个键”?
-
@AdeelMiraj 是的,完全正确!这就是我想要的。
-
在我看来,枚举数组将是相对更容易和直接的方式来做到这一点。通过谓词这样做有什么特别的原因吗?
-
@AdeelMiraj... 我这样做的原因是因为我对表格视图中的各种单元格有冗余逻辑。因此,我不想增加不必要的代码逻辑。另外,由于我是 iOS 新手,我想使用可用的 NSPredicate 系统来实现这个目标。
标签: objective-c nsdictionary nspredicate