在iOS中的字典中使用字典数组的NSPredicate进行过滤

Posted

技术标签:

【中文标题】在iOS中的字典中使用字典数组的NSPredicate进行过滤【英文标题】:Filter using NSPredicate of Array of Dictionary within dictionary in iOS 【发布时间】:2018-03-28 19:02:57 【问题描述】:
Rewards -- Array
   Normal Reward -- Object
     Offer -- Dictionary
       Meta -- Dictionary
         rewardId -- String
   Normal Reward -- Object
     Offer -- Dictionary
       Meta -- Dictionary
         rewardId -- String

我有上面的层次结构,我必须匹配一个特定的 rewardId 以获得一个值。如何使用 NSPredicate 实现这一点。

【问题讨论】:

[NSPredicate predicateWithFormat:@"SELF.nameOfOfferProperty.nameOfMetaKey.rewardId == %@", myRewardIdToMatch]? 【参考方案1】:

这个谓词应该可以解决问题:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.nameOfOfferProperty.nameOfMetaKey.nameOfRewardIdKey == %@", myRewardIdToMatch]

我根据您的需要明确命名了要走的路径。

使用示例代码(这样您就可以看到需要时使用的名称):

NSMutableArray *rewards = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 10; i ++)

    NSString *rewardId = [NSString stringWithFormat:@"rewardID %@", (i%2 == 0)?@"Target":@"NonWanted"];
    NSString *otherMetaValue = [NSString stringWithFormat:@"otherMetaValue-%ld", i];
    NSString *otherOfferValue = [NSString stringWithFormat:@"otherOfferValue-%ld", i];
    NSDictionary *anOfferDict = @@"meta": @@"rewardId": rewardId,
                                             @"otherMetaKey": otherMetaValue,
                                  @"otherOfferKey": otherOfferValue;
    Reward *aReward = [[Reward alloc] initWithOfferDict:anOfferDict andIntValue:i];
    [rewards addObject:aReward];

NSLog(@"Rewards: %@", rewards);
NSString * myRewardIdToMatch = @"rewardID Target";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.offer.meta.rewardId == %@", myRewardIdToMatch];


NSArray *filteredRewards = [rewards filteredArrayUsingPredicate:predicate];
NSLog(@"FilteredRewards: %@", filteredRewards);

还有

@implementation Reward
-(id)initWithOfferDict:(NSDictionary *)dict andIntValue:(NSUInteger)intV

    self = [super init];
    if (self)
    
        _offer = dict;
        _intV = intV;
    
    return self;

覆盖 description 可能会有所帮助,并说明它是否有效:

-(NSString *)description

    return [NSString stringWithFormat:@"<%@ %p> with IntValue: %ld and rewardID:\n%@", [self class], self, _intV,  _offer[@"meta"][@"rewardId"]];


@end

输出:

$> Rewards: (
    "<Reward 0x60000022ce80> with IntValue: 0 and rewardID: rewardID Target",
    "<Reward 0x60000022ce00> with IntValue: 1 and rewardID: rewardID NonWanted",
    "<Reward 0x60000022cf80> with IntValue: 2 and rewardID: rewardID Target",
    "<Reward 0x60000022cea0> with IntValue: 3 and rewardID: rewardID NonWanted",
    "<Reward 0x60000022cf20> with IntValue: 4 and rewardID: rewardID Target",
    "<Reward 0x60000022ce40> with IntValue: 5 and rewardID: rewardID NonWanted",
    "<Reward 0x60000022cfa0> with IntValue: 6 and rewardID: rewardID Target",
    "<Reward 0x60000022cfc0> with IntValue: 7 and rewardID: rewardID NonWanted",
    "<Reward 0x60000022ce60> with IntValue: 8 and rewardID: rewardID Target",
    "<Reward 0x60000022cec0> with IntValue: 9 and rewardID: rewardID NonWanted"
)
$> FilteredRewards: (
    "<Reward 0x60000022ce80> with IntValue: 0 and rewardID: rewardID Target",
    "<Reward 0x60000022cf80> with IntValue: 2 and rewardID: rewardID Target",
    "<Reward 0x60000022cf20> with IntValue: 4 and rewardID: rewardID Target",
    "<Reward 0x60000022cfa0> with IntValue: 6 and rewardID: rewardID Target",
    "<Reward 0x60000022ce60> with IntValue: 8 and rewardID: rewardID Target"
)

【讨论】:

您介意说出原因吗?有没有崩溃?那么崩溃日志呢?你用了什么?你的物品看起来像我的吗?不同的?你尝试了什么?你可以看到我的代码在运行,那么我的和你的有什么不同呢?

以上是关于在iOS中的字典中使用字典数组的NSPredicate进行过滤的主要内容,如果未能解决你的问题,请参考以下文章

如何根据ios中的特定键从数组中获取字典

在iOS9 swift中的字典类型中添加数组作为键和值

iOS 中的 Alamofire 正在接收字典而不是数组

iOS:如何在一个数组中的多个字典中分隔键值?

如何从IOS中的字典数组中删除重复性? [关闭]

在PHP中按数组键的值对字典中的数组进行排序