Reputation: 2007
All,
In a mutable Array I have multiple Arrays, In each Array i have multiple dictionaries, In the Dictionaries i have common Values, Based on the Values I have to Group an Array/Dictionary below is sample Array
(
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fifth;
},
),
(
{
name = "attribute_Subject";
value = Maths;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = Science;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Sixth;
},
),
(
{
name = "attribute_Subject";
value = Maths;
},
{
name = "attribute_class";
value = Sixth;
},
),
)
If you see the Arrays We have English in Three Arrays, I want that three Arrays in a separate Array and same for "Science" and Maths
Can we use predicates?
Can any one help
Upvotes: 0
Views: 182
Reputation: 40
I am not entirely sure I understand what you are asking for, but I think this should help:
Objective-C:
//1
NSMutableArray* englishArray = [[NSMutableArray alloc]init];
NSMutableArray* scienceArray = [[NSMutableArray alloc]init];
NSMutableArray* mathArray = [[NSMutableArray alloc]init];
//2
for(int i = 0; i < mainArray.count; i++){
//3
if([mainArray[i][0][@"value"] isEqual: @"English"]){
//4
[englishArray addObject:mainArray[i]];
}
else if([mainArray[i][0][@"value"] isEqual: @"Science"]){
[scienceArray addObject:mainArray[i]];
}
else if([mainArray[i][0][@"value"] isEqual: @"Math"]){
[mathArray addObject:mainArray[i]];
}
}
Explanation of above code:
NSMutableArrays
to store the three subject arrays.eg. mainArray[i] :
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fourth;
},
),
3.(cont.) Then access 1st element in that array we just accessed. (the first element is the dictionary which contains the subject value)
eg. mainArray[i][0] :
{
name = "attribute_Subject";
value = English;
},
3.(cont.2)Then access the value for key @"value" of that dictionary.
eg. mainArray[i][0][@"value"]:
@"English"
4.If that key is equal to the one we need, add the array to appropriate subject array.
Upvotes: 1
Reputation: 130
I think this code will be helpful. NSLog(@"%@", muteArray);
NSArray *valuesArray = [muteArray valueForKey:@"value"];
NSMutableArray *valuesArray1 = [[NSMutableArray alloc]init];
for(NSArray *arr in valuesArray) {
[valuesArray1 addObject:[arr objectAtIndex:0]];
}
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:valuesArray1];
NSArray *arrayWithoutDuplicates = [orderedSet array];
NSArray *filtered = [muteArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"value Contains[d] %@", [arrayWithoutDuplicates objectAtIndex:0]]];
After getting arrayWithoutDuplicates using for loop you can get separate arrays.
Upvotes: 0