Challenge: Stock Portfolio Value Problem *SOLVED*

Hi everyone, I’ve found most of the challenges easy so far but I can’t figure out why this won’t work.
I created a mutable array property and a value method declaration.
BNRPortfolio.h

...
@property (nonatomic) NSMutableArray *holdings;
- (unsigned int)currentValue;
...

The method should loop through the assets held in the array and add their share prices to ‘sum’.
BNRPortfolio.m

...
- (unsigned int)currentValue
{
    unsigned int sum = 0;
    
    for (BNRStockHolding *s in _holdings) {
        sum += s.currentSharePrice;
        NSLog(@"WORKING..."); // Check to see if anything is happening
    }
    return sum;
}
...

And finally in main I created a few stocks and added them to my portfolio, logging the total value.
main.m

...
BNRPortfolio *portfolio = [[BNRPortfolio alloc] init];
        [portfolio addToHoldings:stockA];
        [portfolio addToHoldings:stockB];
        [portfolio addToHoldings:stockC];
        NSLog(@"current value = %d", portfolio.currentValue);
...

Great! The output shouldn’t disappoint.

Oh…
The ‘WORKINNG’ message isn’t showing which means the fast enumeration in the implementation isn’t working properly.
Any help?

Not sure, but I used the getter, (in your case ‘holdings’) to iterate in the currentValue method, like this:

for (BNRStockholding *s in self.holdings) {

}
I’m told ‘real programmers’ typically only use the instance variable (_holding) in making your own getters or setters.
Also, (this is not currently your problem, but) currentValue is a method, but you are treating it as if it were a property. Try:

NSLog(@“current value = %d”, [portfolio currentValue]);

By the way, I used floats not unsigned ints.
Hope this helps.

Wise words, but still nothing’s happening.
Any other advice?

[quote]Stock A added to portfolio.
Stock B added to portfolio.
Stock C added to portfolio.
Portfolio’s current value = 0 [color=#FF0000]// Not even the WORKING message is displayed.[/color]
Stock A: number of shares = 40 purchase price = 23.00 and current price = 18.00 // All the stocks have legitimate values
…[/quote]
Add a log statement to the - (unsigned int)currentValue method:

[code]- (unsigned int)currentValue
{
NSLog (@"–> %s: count: %d _holdings: %p", PRETTY_FUNCTION, [_holdings count], _holdings);

unsigned int sum = 0;

for (BNRStockHolding *s in _holdings) {
    sum += s.currentSharePrice;
    NSLog(@"WORKING..."); // Check to see if anything is happening
}
return sum;

}[/code]
Check the values of count and _holdings.
If both are zero, make sure that _holdings is initialised properly before any objects are added to it.

Thanks bex that solved it!
I just added a little…

{
    // CHECK IF _holdings IS NIL!
    if (!_holdings) {
        _holdings = [[NSMutableArray alloc] init];
    }
    [self.holdings addObject:s];
    NSLog(@"%@ added to portfolio.", [s description]);
}

And it flows smoothly, thank you :slight_smile:

BTW
You are only summing up the currentSharePrices. Instead, I think stock.valueInDollars should be summed up - which is (currentSharePrice * numberOfShares)