从NSArray中检索NSDictionary,其中字典键的值为X.

Pau*_*len 10 objective-c nsdictionary nsarray ios

我有一个NSArrayNSDictionaries.其中一个数组中的一个字典键包含一个值.我想检索NSDictionary具有该值的.

我的阵列:

Array: (
        {
        DisplayName = "level";
        InternalName = "Number 2";
        NumberValue = 1;
    },
        {
        DisplayName = "PurchaseAmount";
        InternalName = "Number 1";
        NumberValue = 3500;
    }
)
Run Code Online (Sandbox Code Playgroud)

所以,我想得到包含DisplayNameset 的字典PurchaseAmount(不区分大小写).

我怎么能做到这一点?

Pau*_*len 18

以下解决了我的问题:

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

向用户Nate索取他对我的问题的评论!

  • 实际上,这个答案和[特别是lindinax的答案](http://stackoverflow.com/a/15831638/119114)都比你接受的答案要好.Bhargavi的回答检查一个键*包含*字符串,但是你的问题要求一个键等于某个值的结果.一般来说,这两者并不是一回事.它可能对您的密钥名称没有影响,但对于其他人来说,它很可能会有所不同. (3认同)

lin*_*nax 9

喜欢[cd]也会这样做

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

<NSArray>(
    {
       DisplayName = PurchaseAmount;
       InternaName = "Number 1";
       NumberValue = 3500;
    }
)
Run Code Online (Sandbox Code Playgroud)


βha*_*avḯ 5

NSPredicate这种方式

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

这里过滤器将保存包含DisplayName设置为PurchaseAmount的字典(不区分大小写)