My Solution


My solution was very similar to others, but I wanted to add the stocks to the array as they were populated. I added the portfolio class and modified the main.m file.

Portfolio.h Note that the header file did not need to know about the StockHolding class so the StockHolding.h file was not imported.

#import <Foundation/Foundation.h>
@interface Portfolio : NSObject

    NSMutableArray *stocksInPortfolio;

@property (strong) NSMutableArray *stocksInPortfolio;



Portfolio.m Here I needed the StockHolding class so I imported the StockHolding.h file.

#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio 
@synthesize stocksInPortfolio;

//Return Portfolio Value
-(float)valueOfPortfolio {
    //Sum all holdings
    float sumOfHoldings = 0;
    //use object type StockHolding from
    //the StockHolding Class
    for (StockHolding *s in stocksInPortfolio) {
        sumOfHoldings += [s valueInDollars];
    return sumOfHoldings;



#import <Foundation/Foundation.h>
#import "StockHolding.h"
#import "ForeignStockHolding.h"
#import "Portfolio.h"

int main(int argc, const char * argv[])

    @autoreleasepool {
        //Create an instance of the Portfolio Object
        Portfolio *heldPortfolio = [[Portfolio alloc]init];
        //Initialize the array in the Portfolio Class
        [heldPortfolio setStocksInPortfolio: [[NSMutableArray array]init]];
        //Create an instance of the StockHolding object
        //and add them to the array in the Portfolio Class
        StockHolding *stk1 = [[StockHolding alloc]init];
        [stk1 setPurchaseSharePrice:2.30];
        [stk1 setCurrentSharePrice:4.50];
        [stk1 setNumberOfShares:40];
        [heldPortfolio.stocksInPortfolio addObject:stk1];
        StockHolding *stk2 = [[StockHolding alloc]init];
        [stk2 setPurchaseSharePrice:12.19];
        [stk2 setCurrentSharePrice:10.56];
        [stk2 setNumberOfShares:90];
        [heldPortfolio.stocksInPortfolio addObject:stk2];
        StockHolding *stk3 = [[StockHolding alloc]init];
        [stk3 setPurchaseSharePrice:45.10];
        [stk3 setCurrentSharePrice:49.51];
        [stk3 setNumberOfShares:210];
        [heldPortfolio.stocksInPortfolio addObject:stk3];
        ForeignStockHolding *stk4 = [[ForeignStockHolding alloc]init];
        [stk4 setPurchaseSharePrice:45.10];
        [stk4 setCurrentSharePrice:49.51];
        [stk4 setNumberOfShares:210];
        [stk4 setConversionRate:.50];
        [heldPortfolio.stocksInPortfolio addObject:stk4];
        //counter to differentiate between stocks in output
        int counter=0;
        for (StockHolding *stock in [heldPortfolio stocksInPortfolio]) {
            float stockCost = [stock costInDollars];
            float stockValue = [stock valueInDollars];
            NSLog(@"Stock %d is valued at %.2f", counter, stockValue);
        NSLog(@"The portfolio has a value of %.2f", [heldPortfolio valueOfPortfolio]);
    return 0;