10

我有一个NSArraywith NSDictionaries。一个数组中的一个字典键包含一个值。我想NSDictionary用该值检索 。

我的数组:

Array: (
        {
        DisplayName = "level";
        InternalName = "Number 2";
        NumberValue = 1;
    },
        {
        DisplayName = "PurchaseAmount";
        InternalName = "Number 1";
        NumberValue = 3500;
    }
)

所以,我想获取包含DisplayName设置为PurchaseAmount(不区分大小写)的字典。

我怎样才能做到这一点?

4

5 回答 5

18

以下解决了我的问题:

NSArray *filtered = [promotions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName == %@)", @"PurchaseAmount"]];
NSDictionary *item = [filtered objectAtIndex:0];

感谢用户 Nate 对我的问题的评论!

于 2013-04-05T10:18:17.230 回答
9

LIKE[cd] 也会这样做

NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName LIKE[cd] %@)", @"purchaseAmount"]];

回来

<NSArray>(
    {
       DisplayName = PurchaseAmount;
       InternaName = "Number 1";
       NumberValue = 3500;
    }
)
于 2013-04-05T10:31:57.267 回答
5

使用NSPredicate这种方式

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName LIKE[cd] 'PurchaseAmount' "];
NSArray *filter = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filter);

此处过滤器将保存您的字典,其中包含设置为 PurchaseAmount 的 DisplayName(不区分大小写)

于 2013-04-05T10:18:09.013 回答
3

用 Swift 回答,

let predicate = NSPredicate(format: "name contains[cd] %@", stringLookingFor)
let newList = data .filteredArrayUsingPredicate(predicate)

数据 nsarray 看起来像这样

[
 {
   name = Carlos,
   total = 9
 },
 {
   name = Willy,
   total = 2
 }
]
于 2015-12-30T01:18:01.513 回答
0
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName == 'level'"];
  NSArray *arrOutput = [[Array filteredArrayUsingPredicate:predicate] mutableCopy];

在上面两行的帮助下,您将获得所有带有 DisplayName = "PurchaseAmount" 的字典

于 2015-07-22T13:08:25.157 回答