One simple add-on to Challenge but can't figure out


#1

For the final log in main.m, I want to include the name of the stock but I can’t figure out an easy way how (I know, it’s just text!). Basically, I want to print this:

“For YHOO, the cost in dollars is __ and the value in dollars is __.” Any thoughts? I tried including a string in the Stockholding instance, but couldn’t get it to work correctly.

Here is my solution otherwise–

Stockholding.h

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

@interface Stockholding : NSObject

// Declare instance variables
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

// Declare properties
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

// Declare methods

  • (float)costInDollars;
  • (float)valueInDollars;
    @end[/code]

Stockholding.m

[code]#import “Stockholding.h”

@implementation Stockholding

// Synthesize Properties
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

// Implement Methods

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

  • (float)valueInDollars
    {
    return [self currentSharePrice] * [self numberOfShares];
    }
    @end[/code]

Main.m

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

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

@autoreleasepool {
    
    // Create instances of the StockHolding class
    Stockholding *YHOO = [[Stockholding alloc]init];
    Stockholding *GOOG = [[Stockholding alloc]init];
    Stockholding *AAPL = [[Stockholding alloc]init];
    
    // Give the instance variables some fake values
    [YHOO setPurchaseSharePrice: 25.54];
    [YHOO setCurrentSharePrice: 44.21];
    [YHOO setNumberOfShares: 200];
    
    [GOOG setPurchaseSharePrice: 11.75];
    [GOOG setCurrentSharePrice: 13.09];
    [GOOG setNumberOfShares: 100];
    
    [AAPL setPurchaseSharePrice: 19.97];
    [AAPL setCurrentSharePrice: 15.50];
    [AAPL setNumberOfShares: 50];
    
    // Create an empty stockholding array
    NSMutableArray *stockArray = [NSMutableArray array];
    
    // Add stockholdings to the array
    [stockArray addObject:YHOO];
    [stockArray addObject:GOOG];
    [stockArray addObject:AAPL];
    
    // Iterate through the stockholding array
    for (Stockholding *s in stockArray) {
        NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", s, [s costInDollars], [s valueInDollars]); // Not sure how to extract company symbol / name!!!
    }
    
}
return 0;

}
[/code]

I could only get the output log to tell me the name of the memory address from the pointer *s (i.e. <Stockholding: 0x1001091c0>) but not the stock name. Thanks!

Best,


#2

Objects don’t really have a name. You name a pointer *pointer but that does not mean that the object, that it points to is applied that name.

If you want your object to have a name, you have to give it that property in the class description and then call it in your code.

In your Stockholding h. you add:

// Declare properties
@property NSString* name; 

On the side: if you use @property and @synthesize you don’t even have to declare the instance variables, Xcode will generate those automatically, when synthesizing the accessor methods.

then synthesize it in Stockholding.m:

[code]#import “Stockholding.h”

@implementation Stockholding

// Synthesize Properties
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares, name;
(…)
[/code]

and then set and call that name im your main.m

[code]
(…)
// Create instances of the StockHolding class
Stockholding *YHOO = [[Stockholding alloc]init];

    // Give the instance variables some fake values
    [YHOO setPurchaseSharePrice: 25.54];
    [YHOO setCurrentSharePrice: 44.21];
    [YHOO setNumberOfShares: 200];
   
    //Give it a name
    [YHOO setName: @"YHOO"];  // with dot-notation: YHOO.name = @"YHOO";
    
    (...)

    // Iterate through the stockholding array
    for (Stockholding *s in stockArray) {
        NSLog(@"For %@, the cost in dollars is %.2f and the value in dollars is %.2f", [s name], [s costInDollars], [s valueInDollars]);  // or s.name in dot-notation.
    }
    
}
return 0;

}[/code]

This is how I would do it. Please note that I’m a newbie myself, so there might be a more elegant solution.


#3

Thats what I was looking for. Good man!


#4

Very helpful, thanks for responding ChJaeger!!! :sunglasses: