Challenge Solution


#1

ForeignStockHolding.m


#import "ForeignStockHolding.h"

@implementation ForeignStockHolding

@synthesize conversionRate;

-(float)costInDollars {
    float cid = [super costInDollars];
    return cid * conversionRate;
}

-(float)valueInDollars {
    float vid = [super valueInDollars];
    return vid * conversionRate;
}
@end

ForeignStockHolding.h


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

@interface ForeignStockHolding : StockHolding
{
    float conversionRate;
}

@property float conversionRate;

@end

main.m

//Create an array to hold all of the stock holdings.
        NSMutableArray *stockHoldings = [NSMutableArray array];
        
        //Create an instance of the StockHolding object
        //and populate the instance variables
        //and add the instance to the array
        StockHolding *stk1 = [[StockHolding alloc]init];
        [stk1 setPurchaseSharePrice:2.30];
        [stk1 setCurrentSharePrice:4.50];
        [stk1 setNumberOfShares:40];
        [stockHoldings addObject:stk1];
        
        StockHolding *stk2 = [[StockHolding alloc]init];
        [stk2 setPurchaseSharePrice:12.19];
        [stk2 setCurrentSharePrice:10.56];
        [stk2 setNumberOfShares:90];
        [stockHoldings addObject:stk2];
        
        StockHolding *stk3 = [[StockHolding alloc]init];
        [stk3 setPurchaseSharePrice:45.10];
        [stk3 setCurrentSharePrice:49.51];
        [stk3 setNumberOfShares:210];
        [stockHoldings addObject:stk3];
        
        ForeignStockHolding *stk4 = [[ForeignStockHolding alloc]init];
        [stk4 setPurchaseSharePrice:45.10];
        [stk4 setCurrentSharePrice:49.51];
        [stk4 setNumberOfShares:210];
        [stk4 setConversionRate:.50];
        [stockHoldings addObject:stk4];
        
        //counter to differentiate between stocks in output
        int counter=0;
        for (StockHolding *stock in stockHoldings) {
            counter++;
            float stockCost = [stock costInDollars];
            float stockValue = [stock valueInDollars];
            
            NSLog(@"Stock %d had a cost of %.2f and is valued at %.2f", counter, stockCost, stockValue);
            
        }     
        
    }
    return 0;
}

#2

Hi,
sorry for my bad english - i hope you can understand me.

[quote]From the Xcode 4.4 release notes:
The compiler automatically calls @synthesize by default for unimplemented @properties[/quote]

In my solution, i don’t @synthesize conversionRate in ForeignStockHolding.m and the result is “0”.

If i type @synthesize … all works fine. Why does the compiler not synthesize the properties automatically?

Thank you

Jens


#3

Hello,
i found a solution / my mistake:

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

  • (float)costInDollars
    {
    float cid = [super costInDollars];
    return cid * [self conversionRate];
    }

  • (float)valueInDollars
    {
    float vid = [super valueInDollars];
    return vid * [self conversionRate];
    }
    @end
    [/code]