Challenge solved (except for dot notation)


#1

I have the challenge working. The only thing that I don’t understand is why I had to use dot notation to make it work. I’d like to use the preferred square brackets, but when I switch to that, I get errors.

Portfolio.h

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

@interface Portfolio : NSObject
{
    //It has one instance variable
    NSMutableArray *stocksHeld;
}

@property NSMutableArray *stocksHeld;

- (float) valueOfPortfolio;

@end

Portfolio.m

#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio

@synthesize stocksHeld;

- (float)valueOfPortfolio
{
    // Sum up the value of stocks held
    float sum = 0;
    for (StockHolding *a in stocksHeld) {
        sum += [a valueInDollars];
    }
    return sum;
}
@end

main.m

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

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

    // Create instances of StockHolding       
    StockHolding *acme = [[StockHolding alloc] init];
    StockHolding *widgetco = [[StockHolding alloc] init];
    StockHolding *wtfinc = [[StockHolding alloc] init];
    ForeignStockHolding *globalintl = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *transworld = [[ForeignStockHolding alloc] init];
    
    // Assign values to the instances
    [acme setPurchaseSharePrice:10];
    [acme setCurrentSharePrice:12];
    [acme setNumberOfShares:20];
    [acme setStockName:"Acme"];
    
    [widgetco setPurchaseSharePrice:20];
    [widgetco setCurrentSharePrice:17];
    [widgetco setNumberOfShares:35];
    [widgetco setStockName:"Widget Co."];

    [wtfinc setPurchaseSharePrice:35];
    [wtfinc setCurrentSharePrice:50];
    [wtfinc setNumberOfShares:10];
    [wtfinc setStockName:"WTF Inc."];

    [globalintl setPurchaseSharePrice:100];
    [globalintl setCurrentSharePrice:200];
    [globalintl setNumberOfShares:2];
    [globalintl setStockName:"Global Int'l"];
    [globalintl setConversionRate:1.5];

    [transworld setPurchaseSharePrice:1000];
    [transworld setCurrentSharePrice:2000];
    [transworld setNumberOfShares:2];
    [transworld setStockName:"TransWorld"];
    [transworld setConversionRate:2];
    
    // Create instance of Portfolio, fill with stocks
    
    Portfolio *myPort = [[Portfolio alloc]init];
    
    myPort.stocksHeld = [NSMutableArray arrayWithObjects:acme, widgetco, wtfinc, globalintl, transworld, nil];
    
    NSLog(@"The total value of the portfolio is $%.2f.", [myPort valueOfPortfolio]);

    for (StockHolding *d in [myPort stocksHeld]) {
       NSLog(@"%i shares of %s cost $%.2f and are now worth $%.2f.", [d numberOfShares], [d stockName], [d costInDollars], [d valueInDollars]);
    }

}
return 0;
}[/code]

The problem comes in at the creation of the array to hold the stocks that make up the Portfolio.

        Portfolio *myPort = [[Portfolio alloc]init];        
        myPort.stocksHeld = [NSMutableArray arrayWithObjects:acme, widgetco, wtfinc, globalintl, transworld, nil];

This works. But if I try to use [myPort stocksHeld] instead of myPort.stocksHeld, I get the error “Expression is not assignable”. It works in the fast enumeration section (i.e. “for (StockHolding *d in [myPort stocksHeld]) {”) but not in the array creation.

Any ideas? My solution is less complex than some of the others, with only one instance variable in Portfolio (i.e. stocksHeld) and one method (i.e. valueOfPortfolio). Maybe it’s too simple?


#2

It is all good – you used dot-notation perfectly there. You could, if you wanted to, rewrite it without dot-notation:


#3

Thanks so much! I think I may have overworked my brain–I should have been able to figure that out.