[英]How to get Unique NSDictionary from NSArray of NSDictionary
I am using Objective C on iOS 9.我在 iOS 9 上使用 Objective C。
I want to get Unique NSDictionaries
from NSArray
of NSDictionary
.我想从
NSDictionaries
的NSArray
获取 Unique NSDictionary
。
I have N
numbers of NSDictionaries
which look like bellow (for example):我有
N
个NSDictionaries
,如下所示(例如):
staff Description Price
***** *********** *****
01 'some string' 9.99
05 'some string' 8.80
01 'some string' 7.45
01 'some string' 4.21
How to achieve my goal from these?如何从这些中实现我的目标?
The unique parameter is staff
.唯一的参数是
staff
。
I want to get Unique NSArray for each, as:我想为每个获得 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 question might look like a possible duplicate of this , but there is a difference, that I don't want sorted data in any manner.这个问题可能看起来像是this 的一个可能重复,但有一个区别,我不希望以任何方式对数据进行排序。 Second, I want array of those dictionaries which key->
staff
is similar.其次,我想要键->
staff
相似的那些字典的数组。
Thanks to 'Mr.T' for providing this link.感谢'T先生'提供此链接。
after digging into that link, i managed to achieve my goal深入研究该链接后,我设法实现了我的目标
//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);
}];
by the way, thank you so much guys for paying attention on my issue.顺便说一下,非常感谢你们关注我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.