Retrieve NSDictionary from NSArray where dictionary key has value X

左心房为你撑大大i 提交于 2019-11-30 12:57:13

LIKE[cd] will also do it

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

returned

<NSArray>(
    {
       DisplayName = PurchaseAmount;
       InternaName = "Number 1";
       NumberValue = 3500;
    }
)

The following solved my problem:

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

Thnx to user Nate for his comment on my question!

Use NSPredicate this way

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

Here filter will hold your dictionaries which contains DisplayName set to PurchaseAmount (case insensitive)

Answer in Swift,

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

The data nsarray looks like this

[
 {
   name = Carlos,
   total = 9
 },
 {
   name = Willy,
   total = 2
 }
]
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName == 'level'"];
  NSArray *arrOutput = [[Array filteredArrayUsingPredicate:predicate] mutableCopy];

with the help of above 2 lines you will get all dictionary with DisplayName = "PurchaseAmount"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!