【问题标题】:How to get Unique NSDictionary from NSArray of NSDictionary如何从 NSDictionary 的 NSArray 中获取唯一的 NSDictionary
【发布时间】:2016-04-13 05:00:51
【问题描述】:

我在 iOS 9 上使用 Objective C。

我想从NSArrayNSDictionary 中获得唯一的NSDictionaries

我有NNSDictionaries 号码,如下所示(例如):

staff     Description   Price
*****     ***********   *****
01       'some string'  9.99
05       'some string'  8.80
01       'some string'  7.45
01       'some string'  4.21

如何从这些实现我的目标?

唯一的参数是staff

我想为每个获取 Unique NSArray,如:

{
        @[
            @[@{@"staff":01,@"description":@"some string",@"price":9.88},
              @{@"staff":01,@"description":@"some string",@"price":7.45},
              @{@"staff":01,@"description":@"some string",@"price":4.21}
            ],
            @[@{@"staff":05,@"description":@"some string",@"price":8.80}
            ],
        ];
    }
 

这个问题看起来可能与this 重复,但有一个区别,我不希望以任何方式对数据进行排序。其次,我想要 key->staff 相似的那些字典的数组。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

感谢'Mr.T'提供this链接。
在挖掘该链接后,我设法实现了我的目标

    //Step:-1 prepare your inputArray having NSDictionaries

    //Step:-2 now,check that how many unique values avilable in inputArray
    NSMutableSet* _info = [[NSMutableSet alloc]init];
    [inputArray enumerateObjectsUsingBlock:^(NSDictionary* info, NSUInteger index, BOOL* stop){
        [_info addObject:[info valueForKey:@"staff"]];
    }];
    NSLog(@"Unique Value:->\n%@",[_info allObjects]);

    //Step:-3 now,run loop till count of unique objects and use NSPredicate to get filtered array like bellow,
    //        you can use for..in.. OR for(int i=0;.....) OR Other but i preffer block
    [_info.allObjects enumerateObjectsUsingBlock:^(NSString* staff, NSUInteger index, BOOL* stop){
        NSMutableDictionary* info = [[NSMutableDictionary alloc]init];
        [info setValue:staff forKey:@"table"];
        NSArray *filtered = [inputArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(staff == %@)", staff]];
        [info setValue:[NSMutableArray arrayWithArray:filtered] forKey:@"data"];

        NSLog(@"\nFiltered Array:->\n%@",info);
    }];

顺便说一句,非常感谢大家关注我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多