0

I am trying to get the total value totalPurchaseSum of an property stockPurchasePrice from all objects in an array arrPurchaseActions.

This are the properties (user.h):

@property (nonatomic, strong) NSMutableArray *arrPurchaseActions;

@property (nonatomic) CGFloat totalSumPurchased;

-(CGFloat)totalSumPurchased:(CGFloat)stockPurchasePrice;

This is the method I am trying to use (User.m):

-(CGFloat)totalSumPurchased:(CGFloat)stockPurchasePrice
{
    CGFloat totalSumPurchased = 0;
    size_t count = [self.arrPurchaseActions count];
    for (int i = 0; i < count; i++) {
            totalSumPurchased = totalSumPurchased +        [[self.arrPurchaseActions[i].stockPurchasePrice];
}

    return totalSumPurchased;
}

Can anybody help me to get this working?

3
  • @rmaddy: I have objects in an array, one of the properties from this object is stockPurchasePrice. I want to take from all objects the stockPurchasePrice and see the value in totalSumPurchased Commented Apr 18, 2014 at 18:34
  • I understand that. And that is what your code appears to do. So what is the problem you are having with the code you posted? We know what you want to do. Tell us what problem you are having. Commented Apr 18, 2014 at 18:36
  • I get the following message in Xcode: property "stockPurchasePrice" not found on object of type "id". Commented Apr 18, 2014 at 18:45

2 Answers 2

2

Try this:

NSNumber * sum = [self.arrPurchaseActions valueForKeyPath:@"@sum.stockPurchasePrice"];
Sign up to request clarification or add additional context in comments.

Comments

0

This line:

totalSumPurchased = totalSumPurchased + [[self.arrPurchaseActions[i].stockPurchasePrice];

needs to be:

totalSumPurchased = totalSumPurchased + [[self.arrPurchaseActions[i] stockPurchasePrice];

You can't use the property accessor with objects of type id (which is what NSArray objectAtIndex: returns). Instead, call the getter method for the property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.