Reputation: 21
I have an NSMutableDictionary as follows:
{ 0 = (1,5,6); 1 = (0,2,6,7); 2 = (1,7,8); 5 = (0,6,10,11); 6 =
(0,1,5,7,11,12)};
in the format of {NSNumber:NSMutableArray}
I want to remove every 0 that is there in every key or the keys for the values of '0'. What is a way to do it?
The expected outcome is:
{ 0 = (1,5,6); 1 = (2,6,7); 2 = (1,7,8); 5 = (6,10,11); 6 =
(1,5,7,11,12)};
I am looking for an elegant solution.
Upvotes: 2
Views: 793
Reputation: 427
You can do like below. This is improvement of other answer with key '0'.
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"0"] = @[@1,@5,@6];
dict[@"1"] = @[@0,@2,@6,@7];
dict[@"2"] = @[@1,@7,@8];
dict[@"5"] = @[@6,@10,@11];
dict[@"6"] = @[@0,@1,@5,@7,@11,@12];
int valueToRemove = 0; //change this value for what you need to remove
for (NSNumber * key in dict.allKeys) {
if([key intValue] == valueToRemove) {
dict[key] = [dict[key] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"intValue != %i",valueToRemove]];
}else{
dict[key] = [dict[key] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"intValue != %i",valueToRemove]];
}
}
NSLog(@"%@",dict);
And your expected output like this with key '0'.
{
0 = ( 1, 5, 6 );
1 = ( 2, 6, 7 );
2 = ( 1, 7, 8 );
5 = ( 6, 10, 11 );
6 = ( 1, 5, 7, 11, 12 );
}
Upvotes: 0
Reputation: 20804
Use this assuming that your NSMutableArray is an NSNumber
array, also the key 0 is removed in this improvement
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
dict[@0] = [NSMutableArray arrayWithObjects:@0,@10, nil];
dict[@1] = [NSMutableArray arrayWithObjects:@0,@10, nil];
dict[@2] = [NSMutableArray arrayWithObjects:@0,@7,@10, nil];
int valueToRemove = 0; //change this value for what you need
for (NSNumber * key in dict.allKeys) {
if([key intValue] == valueToRemove) {dict[key] = nil;}
dict[key] = [dict[key] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"intValue != %i",valueToRemove]];
}
NSLog(@"%@",dict);
2017-07-28 02:18:21.257 ArrayProblemQuestion[76557:1576267] {
1 = (
10
);
2 = (
7,
10
); }
if you want to loop only for certain keys then
NSMutableArray * array = [NSMutableArray arrayWithObjects:@1,@3,@0, nil];
for (NSNumber * key in [dict.allKeys filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"intValue IN %@",array]]) {
NSLog(@"%@",key);
}
this will loop only for the keys contained in array
Hope this helps
Upvotes: 1
Reputation: 1332
I'd do something like this..
[myDict enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key,
NSMutableArray * _Nonnull array,
BOOL * _Nonnull stop) {
[array removeObject:@0];
}];
Upvotes: 0
Reputation: 5186
// This is your input
NSMutableDictionary *dicMain = [NSMutableDictionary new];
dicMain[@"0"] = @[@1,@5,@6];
dicMain[@"1"] = @[@0,@2,@6,@7];
dicMain[@"2"] = @[@1,@7,@8];
dicMain[@"5"] = @[@0,@6,@10,@11];
dicMain[@"6"] = @[@0,@1,@5,@7,@11,@12];
NSLog(@"%@",dicMain);
// Remove key from Dic if its having 0
if([[dicMain allKeys] containsObject:@"0"])
[dicMain removeObjectForKey:@"0"];
// Check each and every object into all key's that if its having 0 then it will remove
NSArray *aryAllKeys = [dicMain allKeys];
for(NSString *strKey in aryAllKeys)
{
NSMutableArray *aryNewValue = [NSMutableArray new];
NSArray *aryKeyValues = [dicMain objectForKey:strKey];
for(NSString *str in aryKeyValues)
{
if([str intValue] != 0)
{
[aryNewValue addObject:str];
}
}
// Set New array
[dicMain setObject:aryNewValue forKey:strKey];
}
NSLog(@"%@",dicMain);
The output of main Dic without removing 0:
{ 1 = ( 2, 6, 7 ); 2 = ( 1, 7, 8 ); 5 = ( 0, 6, 10, 11 ); 6 = ( 0, 1, 5, 7, 11, 12 ); }
After Removing 0: { 1 = ( 2, 6, 7 ); 2 = ( 1, 7, 8 ); 5 = ( 6, 10, 11 ); 6 = ( 1, 5, 7, 11, 12 ); }
Upvotes: 0