My challenge solution, is there a better way?


#1

Here is my go. A question is what is the value of [self]? It seems like I can go without it.

[code]
//Here is my .h file
#import <Foundation/Foundation.h>

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

  • (float)costInDollars;
  • (float)valueInDollars;

@end[/code]

[code]
//Here is my .m file
#import “StockHolding.h”

@implementation StockHolding
@synthesize purchaseSharePrice,currentSharePrice,numberOfShares;

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

@end[/code]

//Here comes main.m
//
//  main.m
//  ObjectC_Practice
//
//  Created by Apple on 13/4/26.
//  Copyright (c) 2013年 nickhsieh. All rights reserved.
//

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

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

    @autoreleasepool {
        NSMutableArray *holdings=[[NSMutableArray alloc] init];
        
        //create 2 instances of stockholding
        StockHolding *one = [[StockHolding alloc] init];
        StockHolding *two = [[StockHolding alloc] init];
        
        //set values for stockholding instances
        [one setPurchaseSharePrice:10];
        [one setCurrentSharePrice:100];
        [one setNumberOfShares:1000];
        [two setPurchaseSharePrice:20];
        [two setCurrentSharePrice:100];
        [two setNumberOfShares:1000];
        //fill in with instances of stockholding
        [holdings addObject:one];
        [holdings addObject:two];
        //iterate through the array the print out
        for (StockHolding *d in holdings)
        {
            float cost=[d costInDollars];
            float currentvalue=[d valueInDollars];
            
            NSLog(@"cost is %f, currentvalue is %f",cost,currentvalue);
        }
        
    }
    return 0;
}

#2

Re-read the chapter again to get a better understanding of self.

Self points to the object it is called in.

Other than making your code a little easier to read - I think the syntax is just fine.


#3

My error message I get is: Type of property ‘purchaseSharePrice’ (‘float’) does not match type of instance variable ‘purchaseSharePrice’ (‘float *’)

Any ideas to fix?

Thanks, Alistair


#4

Make them both of the same type: float.


#5

Just float without * didn’t work. That made it 6 errors instead of two errors.

Error 1: Type of property ‘purchaseSharePrice’ (‘float’) does not match type of instance variable ‘purchaseSharePrice’ (‘float *’)

Error 2: Type of property ‘currentSharePrice’ (‘float’) does not match type of instance variable ‘currentSharePrice’ (‘float *’)

Error 3: Expected ‘]’

Error 4: Missing ‘[’ at start of message send expression

Error 5: Expected ‘]’

Error 6: Missing ‘[’ at start of message send expression

Any ideas?


#6

Apologies that when I posted errors the other day I left out the second error. They were:

Type of property ‘purchaseSharePrice’ (‘float’) does not match type of instance variable ‘purchaseSharePrice’ (‘float *’)

Type of property ‘currentSharePrice’ (‘float’) does not match type of instance variable ‘currentSharePrice’ (‘float *’)