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
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
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.
[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:
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.
BTW
You are only summing up the currentSharePrices. Instead, I think stock.valueInDollars should be summed up - which is (currentSharePrice * numberOfShares)