sum of all same keys iOS

I am new to iOS,

I have one NSMutableArray of NSdictionary,

I want to do sum of all same keys, Here is my code i have tried but doesn't success

ARQProfileDataArray(this is NSMutableArray):

{
"type":"arqEquity",
"category":"Equity",
"investment":"6935.7",
"percentage":"25.43"
},
{
"type":"liquid",
"category":"MF",
"investment":0,
"percentage":0
},
{
"type":"balanced",
"category":"MF",
"investment":"10215.87",
"percentage":"37.46"
},
{
"type":"equityMF",
"category":"MF",
"investment":"10123.41",
"percentage":"37.12"
}

I want to sum of all category which conatins MF as well sum of all category which conatins Equity

My Code:

 NSNumber *sum = [ARQProfileDataArray valueForKeyPath:@"@sum.category"];
  NSLog(@"sum---------- %@",sum);

My code crashes on this line NSNumber *sum = [ARQProfileDataArray valueForKeyPath:@"@sum.category"];

Please help where i am making mistake? Thanks in advance!!

Upvotes: 0

Views: 79

Answers (2)

Vignesh Kumar
Vignesh Kumar

Reputation: 598

Please find a simple and generic solution.

    NSMutableArray *sumArray = [@[@{
        @"type":@"arqEquity",
        @"category":@"Equity",
        @"investment":@6935.7,
        @"percentage":@"25.43"
    },
    @{
        @"type":@"liquid",
        @"category":@"MF",
        @"investment":@0,
        @"percentage":@0
    },
    @{
        @"type":@"balanced",
        @"category":@"MF",
        @"investment":@10215.87,
        @"percentage":@"37.46"
    },
    @{
        @"type":@"equityMF",
        @"category":@"MF",
        @"investment":@10123.41,
        @"percentage":@"37.12"
        }] mutableCopy];

    // It will work for Number and Integer.
    NSNumber *sum = [sumArray valueForKeyPath:@"@sum.investment"];
    NSLog(@"Sum of Investment : %@", sum);

    NSArray *categorys = [sumArray valueForKeyPath:@"@distinctUnionOfObjects.category"];
    for (NSString* item in categorys)
    {

        NSArray *categoryArray = [self findSameValueKey:sumArray WithKey:@"category" WithValue:item];
        NSLog(@"%@ count: %@", item,@([categoryArray count]));
    }


    // Helps to find same value key.
    -(NSArray*)findSameValueKey:(NSArray*)dateArray WithKey:(NSString*)key WithValue:(NSString*)value{
        NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *resultsDictionary, NSDictionary *bind){
            if ([resultsDictionary objectForKey: key] && [[resultsDictionary objectForKey: key] isEqualToString:value]) {
                return true;
            }
            return false;
        }];

        // Apply the predicate block .
        NSArray *sameValueKeys = [dateArray filteredArrayUsingPredicate:predicate];
        return sameValueKeys;
    }

Upvotes: 0

Dharma
Dharma

Reputation: 3013

Make Looping your NSMutableArray. Please check the syntax of Objective-C its long time & i didn't checked

//Declare variable for category types

int mfCount = 0
int equityCount = 0

for dict in ARQProfileDataArray {
  if ([dict[@"category"] isEqualToString:@"MF"]) {
       mfCount += 1;
  } else if ([dict[@"category"] isEqualToString:@"Equity"]) {
       equityCount += 1;
 }
}

Now you have sum of categories in mfCount & equityCount .

If you don't wanna make variable for each key then you can probably make dictionary instead & update it inside of cases.

Upvotes: 1

Related Questions