Help! Stuck in challenge


I started the challenge like this:

#import <Foundation/Foundation.h>

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

@property float purhcaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;


#import "StockHolding.h"

@implementation StockHolding

@synthesize purhcaseSharePrice, currentSharePrice, numberOfShares;

    return purhcaseSharePrice * numberOfShares;

    return currentSharePrice * numberOfShares;

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

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

    @autoreleasepool {
        NSMutableArray *purchasePrice = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:2.30], 
                                                                        [NSNumber numberWithFloat:12.19], 
                                                                        [NSNumber numberWithFloat:45.10], 
        NSMutableArray *currentPrice = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:4.50],
                                                                        [NSNumber numberWithFloat:10.56],
                                                                        [NSNumber numberWithFloat:90],
        NSMutableArray *numberOfShares = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:40],
                                                                          [NSNumber numberWithInt:90],
                                                                          [NSNumber numberWithInt:210],
        //fast enumeration of array
        for (NSMutableArray *p in purchasePrice) {
            NSLog(@"Purchase Price: %@", p);

        for (NSMutableArray *n in currentPrice) {
            NSLog(@"Current Price: %@", n);

         for (NSMutableArray *d in numberOfShares) {
         NSLog(@"Number of Shares: %@", d);

    return 0;

Now I dont know how to end it?
Did I started it wrong?
Is it too complex?
Shall I abandon & do it in another way?



You appear to have misread the requirements of the challenge, as you have created three arrays populated with the instance variables, rather than creating a single array which is populated with three instances of your StockHolding class - the diagram on page 107 may help to clarify the requirements.



My bad.
Thanks for pointing that out.

Here’s the new one.
Still have to figure out printing in array.
Slowly getting there.

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

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

    @autoreleasepool {
        //create an instance of StockHolding
        StockHolding *stock1 = [[StockHolding alloc]init];
        StockHolding *stock2 = [[StockHolding alloc]init];
        StockHolding *stock3 = [[StockHolding alloc]init];
        //fill the stock1 instance with data
        [stock1 setPurchaseSharePrice:2.30];
        [stock1 setCurrentSharePrice:4.50];
        [stock1 setNumberOfShares:40];
        //fill the stock2 instance with data
        [stock2 setPurchaseSharePrice:12.9];
        [stock2 setCurrentSharePrice:10.56];
        [stock2 setNumberOfShares:90];
        //fill the stock3 instance with data
        [stock3 setPurchaseSharePrice:45.10];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberOfShares:210];
        float cod1 = [stock1 costInDollars];
        float vid1 = [stock1 valueInDollars];

        float cod2 = [stock2 costInDollars];
        float vid2 = [stock2 valueInDollars];
        float cod3 = [stock3 costInDollars];
        float vid3 = [stock3 valueInDollars];
        //print stocks data
        NSLog(@"Stock1 cost in dollar is %.2f and value in dollars is %.2f.", cod1, vid1);
        NSLog(@"Stock2 cost in dollar is %.2f and value in dollars is %.2f.", cod2, vid2);
        NSLog(@"Stock3 cost in dollar is %.2f and value in dollars is %.2f.", cod3, vid3);       
    return 0;


No problem, and congrats on figuring it out.

If you wish to know how to do the array part then read on:

//First you want to create the array and populate it with your StockHolding instances
NSArray *stocksArray = [NSArray arrayWithObjects:stock1, stock2, stock3, nil];

// Then using fast enumeration you can iterate through the array, printing out the values
for (StockHolding *holding in stocksArray)
    NSLog(@"%.02f", [holding costInDollars]);
    NSLog(@"%.02f", [holding valueInDollars]);



Thank you very much for helping me out.
I appreciate it.

Copied this code from adex0909 & it printed out well.

        NSMutableArray *arrayOfStocks = [NSMutableArray arrayWithObjects:stock1, stock2, stock3, nil];
        for (int i = 0; i <= 2; i++) {
        NSLog(@"%.2f %.2f" ,[[arrayOfStocks objectAtIndex:i] costInDollars], [[arrayOfStocks objectAtIndex:i] valueInDollars]);

Problem is I want it printed out like “Stock1 cost in dollar is X, and value in dollar is Y”.
Any ideas?


on second thought, never mind.
I should focus on moving on & not get tied up in making the print out “pretty”.

Many, many thanks.

NSLog(@"Stock %i cost in dollar is %.2f, and the value is %.2f",
                  i + 1, [[arrayOfStocks objectAtIndex:i] costInDollars],
                  [[arrayOfStocks objectAtIndex:i] valueInDollars])

Array indexing starts at 0, so you need to use i + 1 to start the numbering at 1.



That’s great!

Thanks much!