Answer to Challenge with detailed comments


Hello all, here is my go at the exercise. I commented it out pretty well i think, and also has a special commented-out section for the purists :=) Enjoy:


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

@interface StockHolding : NSObject

// Here we declare the instance variables inside curly brackets
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;

// We use the @property directive, to have the compiler create accessor methods for us, so we can set and get the values of our instance variables
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

// These are our instance methods, meaning methods that will be called on instances of this class. They are only declared here, and will be defined in the .m file
-(float) valueInDollars;
-(float) costInDollars;
-(int) numberOfShares;



[code]#import “StockHolding.h”

@implementation StockHolding

// We synthesize our @property declarations to create our set & get methods
@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

// Here we implement the methods, meaning we tell them exactly how to operate and what to do when called
-(float) costInDollars {
return purchaseSharePrice * numberOfShares;

-(float) valueInDollars {
return numberOfShares * currentSharePrice;

/* To satisfy the purists: We never directly access our instance variables. We use the variable self, which is a pointer to the receiver of this message, to access it’s particular instance variables.

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

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



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

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

@autoreleasepool {
    // Declare our pointer variables, which tells the compiler that they will point to an instance of the class on the heap. They actually contains the address of the instance, and since we called alloc 3 times, we now have 3 different instances of the StockHolding class, with 1 pointer variable to point to each one of them
    StockHolding *stockOne = [[StockHolding alloc] init];
    StockHolding *stockTwo = [[StockHolding alloc] init];
    StockHolding *stockThree = [[StockHolding alloc] init];
    // The pointer variable knows where the object is, and therefore it can send messages to it's particular instance. We use that here to set the instance variables with our synthesized setter methods. The first part inside the brackets is the receiver of the message, and the next is the name of the method being sent, with an argument. What we defined for the message in the .m file will now tell the compiler what happens here
    [stockOne setNumberOfShares:40];
    [stockOne setPurchaseSharePrice:2.30];
    [stockOne setCurrentSharePrice:4.50];
    [stockTwo setNumberOfShares:90];
    [stockTwo setPurchaseSharePrice:12.19];
    [stockTwo setCurrentSharePrice:10.56];
    [stockThree setNumberOfShares:210];
    [stockThree setPurchaseSharePrice:45.10];
    [stockThree setCurrentSharePrice:49.51];
    // Now that we have 3 instances of our class, we will add all these instances to an array. Or actually, we send the address of those instances, and have the array point to them, so that it knows where they live on the heap as well. Same principle as with the pointer variables.
    NSMutableArray *myStocks = [[NSMutableArray alloc] initWithObjects:stockOne, stockTwo, stockThree, nil];
    // We use the fast enumeration to cycle through our array, and print out information on each of our instances
    for (StockHolding *stock in myStocks)
        NSLog (@"Stock number: %lu\nYou own: %i\nPurchased for a total of: $%.2f\nCurrent value: $%.2f", [myStocks indexOfObject:stock] + 1, [stock numberOfShares], [stock costInDollars], [stock valueInDollars]);
return 0;


Best Regards


Hi there, thanks for the code, it is well documented indeed. Just one question: In your StockHolding.h is there a need for your last line of code: “-(int) numberOfShares;” ?


Looking at the code I would say that the method isn’t needed as it’s never implemented in StockHolding.m.

Removing it will make the incomplete implementation warning in Xcode go away.


Thanks for your detailed answer. :slight_smile: