One way of doing the challenge


My solution seems to work. Let me know what you think.


[code]#import <Foundation/Foundation.h>
@class StockHolding; //Wondering if I should import instead of @class.

@interface Portfolio : NSObject
NSMutableArray *portfolioOfStocks;

@property (strong) NSMutableArray *portfolioOfStocks;

-(void)addStockToPortfolio:(StockHolding *)newStock;



[code]#import “Portfolio.h”
#import “StockHolding.h” //Wonder if I could get away with @class???

@implementation Portfolio

@synthesize portfolioOfStocks;

-(void)addStockToPortfolio:(StockHolding *)newStock
//Add stocks, but first create the array if one does not exist.
if (!portfolioOfStocks) {
portfolioOfStocks = [[NSMutableArray alloc] init];
//An array already exists, so just add to it.
[portfolioOfStocks addObject];

float portfolioValue = 0;
for (StockHolding *s in portfolioOfStocks){
float thisStockValue = [s valueInDollars];
portfolioValue = portfolioValue + thisStockValue;
return portfolioValue;


[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface ForeignStockHoldings : StockHolding
float conversionRate;

@property float conversionRate;




[code]#import “ForeignStockHoldings.h”

@implementation ForeignStockHoldings

@synthesize conversionRate;

return purchaseSharePrice * numberOfShares * conversionRate;

return currentSharePrice * numberOfShares * conversionRate;



[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars; //purchaseSharePrice * numberOfShares
-(float)valueInDollars; //currentSharePrice * numberOfShares



[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

return purchaseSharePrice * numberOfShares;

return currentSharePrice * numberOfShares;



[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHoldings.h”
#import “Portfolio.h”

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

@autoreleasepool {
    //Create a Portfolio.
    Portfolio *myAwesomePortfolio = [[Portfolio alloc] init];
    StockHolding *stock1 = [[StockHolding alloc] init];
    [stock1 setNumberOfShares:50];
    [stock1 setPurchaseSharePrice:1.90];
    [stock1 setCurrentSharePrice:2.90];
    StockHolding *stock2 = [[StockHolding alloc] init];
    [stock2 setNumberOfShares:100];
    [stock2 setPurchaseSharePrice:2.90];
    [stock2 setCurrentSharePrice:3.90];
    StockHolding *stock3 = [[StockHolding alloc] init];
    [stock3 setNumberOfShares:200];
    [stock3 setPurchaseSharePrice:2.90];
    [stock3 setCurrentSharePrice:1.90];

    //Create the foreign stocks.
    ForeignStockHoldings *stock4 = [[ForeignStockHoldings alloc] init];
    [stock4 setNumberOfShares:1000];
    [stock4 setPurchaseSharePrice:1.00];
    [stock4 setCurrentSharePrice:2.00];
    [stock4 setConversionRate:2.00];
    ForeignStockHoldings *stock5 = [[ForeignStockHoldings alloc] init];
    [stock5 setNumberOfShares:1];
    [stock5 setPurchaseSharePrice:1000.0];
    [stock5 setCurrentSharePrice:900.0];
    [stock5 setConversionRate:.2];
    [myAwesomePortfolio addStockToPortfolio:stock1];
    [myAwesomePortfolio addStockToPortfolio:stock2];
    [myAwesomePortfolio addStockToPortfolio:stock3];
    [myAwesomePortfolio addStockToPortfolio:stock4];
    [myAwesomePortfolio addStockToPortfolio:stock5];
    float myAwesomeValue = [myAwesomePortfolio valueofPortfolio];
    NSLog(@"My portfolio's value is %.2f", myAwesomeValue);
return 0;


I know, I know. I forgot to capitalize “Of” in the method “valueofPortfolio”. Also, the “costInDollars” method is just left over from the prior challenge. It does work, though. Any thoughts? Any way to improve???


You’re synthesizing two methods for an array (a setter and a getter) with the @property and @synthesize directives on portfolioOfStocks. I’d remove that. You don’t attempt to use these synthetic methods here, so you won’t get any unwelcome results in this program.


True. I’m really weak on memory management. I was thinking that I needed to declare the Portfolio’s ownership of the array . . . Like I said, really weak on memory management. :slight_smile: Can you (should you) do this in the header?

@interface Portfolio : NSObject { __strong NSMutableArray *portfolioOfStocks; }


In this case, no.
When you declare an object instance variable as such:

@interface Portfolio : NSObject
NSMutableArray *portfolioOfStocks;

The variable is strong by default. You can use __strong there, but it isn’t necessary.

However, if you DO need generic setter/getter methods using @property/@synthesize, it is NOT strong by default, (the default attribute would be unsafe_retained) and would be declared in a manner such as

@property (strong, nonatomic) NSMutableArray *portfolioOfStocks;

and you would not need to declare it at the top of the interface section between the { }.

When you send the message “addStockToPortfolio” to myAwesomePortfolio the first time it recognizes that it needs an array and uses: portfolioOfStocks = [[NSMutableArray alloc] init];
Calling alloc here, ownership is now given to the receiver (myAwesomePortfolio).
As a side note, portfolioOfStocks = [NSMutableArray array] also works to allocate and initialize.
Hope this helps clarify my first fairly vague answer :smiley: