This is called PERFECT GRILLIN!


Iam a proud owner of this BOOK and I just want to say that I haven’t seen anyone delivering such a complicated Object Oriented Approach any better than in this chapter.

        Portfolio *myPortfolioOfStocks=[[Portfolio alloc] init];
        for(int i=0;i<5;i++)
            StockHolding *s=[[StockHolding alloc] init];
            [myPortfolioOfStocks addStocks:s];
        NSLog(@"My Portfolio contains: %lu Stock(s), their value is: %0.2f and their total cost is: %0.2f",
              [myPortfolioOfStocks totalStockInArray],
              [myPortfolioOfStocks totalStocksValue],
              [myPortfolioOfStocks totalStocksCost]);

#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface Portfolio : NSObject
NSMutableArray *stocks;
float totalStocksValueCount;
float totalStocksCostCount;



I think I should have these two “totalStocksValueCount” and “totalStocksCost” as local vars instead of global, so that they are only created when the method is called and destroyed from the memory rightaway !!

#import “Portfolio.h”

@implementation Portfolio

stocks=[[NSMutableArray alloc]init];
[stocks addObject:s];
for(StockHolding* s in stocks){
totalStocksValueCount+=[s valueInDollars];
return totalStocksValueCount;
for(StockHolding* s in stocks){
totalStocksCostCount+=[s costInDollars];
return totalStocksCostCount;
return [stocks count];

Because we have two methods in StockHolding, one which calculates the value and other cost from previous chapters. I have used two diff methods inside Portfolio.m to retrieve sum of each.
There it is:
2011-11-16 22:17:00.786 Learning1[1171:707] My Portfolio contains: 5 Stock(s), their value is: 76.00 and their total cost is: 46.00


Thanks a bunch! I’ve been teaching this stuff a while now, and this book was a very exciting effort to get my approach on paper. I’m glad it is working for you.


Looking at your header I saw those two floats declared.

float totalStocksValueCount; float totalStocksCostCount;

You shouldn’t need those at all since they are fully encapsulated by the class. Case in point you don’t synthesize or make any accessors for them in Portfolio.m.