Solution for Challenge


Here is my solution I hope its helpful, If you find a better way I would be very interested in seeing it.

main.m file

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

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

    @autoreleasepool {
        //Create an array to hold all of the stock holdings.
        NSMutableArray *stockHoldings = [NSMutableArray array];
        //Create an instance of the StockHolding object
        //and populate the instance variables
        //and add the instance to the array
        StockHolding *stk1 = [[StockHolding alloc]init];
        [stk1 setPurchaseSharePrice:2.30];
        [stk1 setCurrentSharePrice:4.50];
        [stk1 setNumberOfShares:40];
        [stockHoldings addObject:stk1];
        StockHolding *stk2 = [[StockHolding alloc]init];
        [stk2 setPurchaseSharePrice:12.19];
        [stk2 setCurrentSharePrice:10.56];
        [stk2 setNumberOfShares:90];
        [stockHoldings addObject:stk2];
        StockHolding *stk3 = [[StockHolding alloc]init];
        [stk3 setPurchaseSharePrice:45.10];
        [stk3 setCurrentSharePrice:49.51];
        [stk3 setNumberOfShares:210];
        [stockHoldings addObject:stk3];
        //counter to differentiate between stocks in output
        int counter=0;
        for (StockHolding *stock in stockHoldings) {
            float stockCost = [stock costInDollars];
            float stockValue = [stock valueInDollars];
            NSLog(@"Stock %d had a cost of %.2f and is valued at %.2f", counter, stockCost, stockValue);
    return 0;

StockHolding.h file

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject

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

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


StockHolding.m file

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice;
@synthesize currentSharePrice;
@synthesize numberOfShares;

-(float)costInDollars {
    return [self purchaseSharePrice] * [self numberOfShares];

-(float)valueInDollars {
    return [self currentSharePrice] * [self numberOfShares];



Thanks for posting your solution

I was attempting to do it in a more complex way - your way is simpler and easier.


My solution was exactly the same, except instead of adding every object to the array explicitly, I used arrayWithObjects and added them to the array in one line: