Challenge 17 Solution


Challenge 17 Solution

To make things easier to read, I’ve put StockHolding.h and StockHolding.m on the same file: main.m

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

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

@implementation StockHolding
@synthesize numberOfShares, currentSharePrice, purchaseSharePrice;
-(float)costInDollars {
return numberOfSharespurchaseSharePrice;
-(float)valueInDollars {
return numberOfShares

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

@autoreleasepool {
    StockHolding *stock1 = [StockHolding new];
    StockHolding *stock2 = [StockHolding new];
    StockHolding *stock3 = [StockHolding new];
    stock1.purchaseSharePrice = 100;
    stock1.currentSharePrice = 98;
    stock1.numberOfShares = 10;
    stock2.purchaseSharePrice = 10;
    stock2.currentSharePrice = 9;
    stock2.numberOfShares = 2;
    stock3.purchaseSharePrice = 1000;
    stock3.currentSharePrice = 980;
    stock3.numberOfShares = 100;
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:stock1, stock2, stock3, nil];
    int i=1;
    for (StockHolding *sh in array) {
        NSLog(@"Stock %i: Cost: $%.2f Value: $%.2f", i, sh.costInDollars, sh.valueInDollars);
return 0;


This is the output:

2013-08-07 21:25:44.293 Challenge 17 Your First Class[876:303] Stock 1: Cost: $1000.00 Value: $980.00
2013-08-07 21:25:44.295 Challenge 17 Your First Class[876:303] Stock 2: Cost: $20.00 Value: $18.00
2013-08-07 21:25:44.296 Challenge 17 Your First Class[876:303] Stock 3: Cost: $100000.00 Value: $98000.00


How does it advance through the array? I’m confused as to why it doesn’t just output the same array object repeatedly.


Hello Fostermatt,

the following syntax you will find at the end of the program (StockHolding *sh in array) enumerates the array basically starting from array[0] to the end of the array. In order for it to properly deal with the objects in the array that were stored there using NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:stock1, stock2, stock3, nil] you need to know the type of objects in the array and since we know they are of class StockHolding, that class is then used to declare a new object *sh and with each enumeration the sh points to the stocks 1,2 and 3 in order. With each iteration and since sh has the address of the stock at iteration i you can use that object to implement a method declared in StockHolding.

Hope this helps.

Happy coding!


My code was much longer, but it still works.

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
int main(int argc, const char * argv[])

    @autoreleasepool {
     //Create an instance of BNRStockHolding
        BNRStockHolding *stock1 = [[BNRStockHolding alloc] init];
        BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
        BNRStockHolding *stock3 = [[BNRStockHolding alloc] init];
        //Give instance variables values
        [stock1 setPurchasePrice:20.98];
        [stock1 setcurrentPrice:50.98];
        [stock1 setNumberOfShares:2];

        [stock2 setPurchasePrice:60.09];
        [stock2 setcurrentPrice:70.98];
        [stock2 setNumberOfShares:9];
        [stock3 setPurchasePrice:2.98];
        [stock3 setcurrentPrice:5.98];
        [stock3 setNumberOfShares:12];

        NSMutableArray *stockArray = [[NSMutableArray alloc]initWithObjects:stock1,stock2,stock3,nil];
        for (BNRStockHolding *allStocks in stockArray){
        // use getters
        float p = [allStocks purchasePrice];
        float c = [allStocks currentPrice];
        float n = [allStocks numberOfShares];
        //use methods
        float cost = [allStocks costInDollars];
        float value = [allStocks valueInDollars];
        NSLog(@"\n\nThe purchase price was %.2f the current price is %.2f the number of shares bought were %.0f the total cost was %.2f and the current value is %.2f\n",p,c,n,cost,value);
    return 0;