我有NSArray
NSDictionaries
。其中一个数组中的一个字典键包含一个值。我想要使用该值检索NSDictionary
。
我的阵列:
Array: (
{
DisplayName = "level";
InternalName = "Number 2";
NumberValue = 1;
},
{
DisplayName = "PurchaseAmount";
InternalName = "Number 1";
NumberValue = 3500;
}
)
因此,我想将包含DisplayName
的字典设置为PurchaseAmount
(不区分大小写)。
我该如何实现?
答案 0 :(得分:18)
以下解决了我的问题:
NSArray *filtered = [promotions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName == %@)", @"PurchaseAmount"]];
NSDictionary *item = [filtered objectAtIndex:0];
向用户Nate提出他对我的问题的评论!
答案 1 :(得分:9)
LIKE [cd]也会这样做
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName LIKE[cd] %@)", @"purchaseAmount"]];
返回
<NSArray>(
{
DisplayName = PurchaseAmount;
InternaName = "Number 1";
NumberValue = 3500;
}
)
答案 2 :(得分:5)
以这种方式使用NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName LIKE[cd] 'PurchaseAmount' "];
NSArray *filter = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filter);
此处过滤器将保存包含DisplayName设置为PurchaseAmount的字典(不区分大小写)
答案 3 :(得分:3)
在Swift中回答,
let predicate = NSPredicate(format: "name contains[cd] %@", stringLookingFor)
let newList = data .filteredArrayUsingPredicate(predicate)
数据nsarray看起来像这样
[
{
name = Carlos,
total = 9
},
{
name = Willy,
total = 2
}
]
答案 4 :(得分:0)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName == 'level'"];
NSArray *arrOutput = [[Array filteredArrayUsingPredicate:predicate] mutableCopy];
在上述2行的帮助下,您将获得DisplayName =“PurchaseAmount”
的所有词典