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

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

我的arrays:

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

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

我怎么能做到这一点?

以下解决了我的问题:

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

thnx用户Nate对我的问题发表评论!

LIKE [cd]也会这样做

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

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

使用NSPredicate这种方式

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

这里的filter将把你的包含DisplayName的字典设置为PurchaseAmount(不区分大小写)

在Swift中回答,

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

数据nsarray看起来像这样

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

在上述2行的帮助下,您将获得DisplayName =“PurchaseAmount”的所有字典