Nested Fast enumeration


#1

Im trying to use nested enumeration to print out to the console the value of each portfolio plus for each portfolio i want to list the stocks that are being held. I’ve used a nested for loop with fast enumeration but it doesn’t work and I have no idea why…

[code] //Create the stock objects and set the values for each
StocksHolding *apple = [[StocksHolding alloc] init];
[apple setName:@“APPL”];
[apple setPurchasedSharePrice:10.34];
[apple setCurrentSharePrice:459.44];
[apple setNumberOfShares:10000];

    ForeignStockHolding *eoan = [[ForeignStockHolding alloc] init];
    [eoan setName:@"e.on n"];
    [eoan setPurchasedSharePrice:51.40];
    [eoan setCurrentSharePrice:17.10];
    [eoan setNumberOfShares:500];
    [eoan setConversionRate:0.80];
     
    StocksHolding *goog = [[StocksHolding alloc] init];
    [goog setName:@"GOOG"];
    [goog setPurchasedSharePrice:75.00];
    [goog setCurrentSharePrice:620.80];
    [goog setNumberOfShares:5000];
    
    
    
    // Create the portfolios
    Portfolio *portfolioOne = [[Portfolio alloc] init];
    Portfolio *portfolioTwo = [[Portfolio alloc] init];
    
    // Give the portfolios unique IDs
    [portfolioOne setPortfolioID:01];
    [portfolioTwo setPortfolioID:02];
    
    // Add some stocks holdings to the portfolios
    [portfolioOne addStocksHolding:apple];
    [portfolioOne addStocksHolding:eoan];
    [portfolioOne addStocksHolding:goog];
    [portfolioTwo addStocksHolding:eoan];
    [portfolioTwo addStocksHolding:apple];
    
    
    
    // Create the array of portfolios
    NSMutableArray *portfolios = [[NSMutableArray alloc] init];
    
    // Add the portfolios to the array of portfolios
    [portfolios addObject:portfolioOne];
    [portfolios addObject:portfolioTwo];
    
    
    for (Portfolio *p in portfolios) {
        
        NSLog(@"The value of portfolio %d is %2.f which has the following stocks:" , [p portfolioID], [p portfolioValue]);
        
        for (StocksHolding *s in p) {
            NSLog(@"Name: %@ ", [s name]);
        } 
    }

[/code]

Any help would be appreciated


#2

You need to specify a collection that you are iterating over. A portfolio has an array, and that array (not the portfolio) is what you want to iterate over.

So, you might have an accessor method to return the array in Portfolio:

- (NSArray *)stockHoldings { return stockHoldings; }
Then you use that method to get the array from the portfolio:

[code] for (Portfolio *p in portfolios) {

        NSLog(@"The value of portfolio %d is %2.f which has the following stocks:" , [p portfolioID], [p portfolioValue]);
        NSArray *stocks = [porfolio stockHoldings];
        for (StocksHolding *s in stocks) {
            NSLog(@"Name: %@ ", [s name]);
        } 
    }[/code]

#3

Thanks for the feedback.

Ok using what you advised I got it working… slightly different to what you said… One of the big reasons was I didn’t have an accessor for the array in the Portfolio class.

Heres the code for my .h and .m portfolio class:

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

@class StocksHolding;

@interface Portfolio : NSObject
{
int portfolioID;
NSMutableArray *portfolio;
}

@property int portfolioID;

  • (NSMutableArray *)portfolio;
  • (void)addStocksHolding:(StocksHolding *) s;
  • (float)portfolioValue;

@end
[/code]

[code]#import “Portfolio.h”
#import “StocksHolding.h”

@implementation Portfolio

@synthesize portfolioID;

  • (NSMutableArray *)portfolio
    {
    return portfolio;
    }

  • (void)addStocksHolding:(StocksHolding *)s
    {

    // Check to see if the portfolio array has been created
    if (!portfolio) {

      // Create the portfolio array
      portfolio = [[NSMutableArray alloc] init];
    

    }

    // Add the stocksholding to the portfolio array
    [portfolio addObject:s];
    }

  • (float)portfolioValue
    {
    float sum = 0;
    for (StocksHolding *a in portfolio) {
    sum += [a valueInDollars];
    }

    return sum;
    }

@end
[/code]

and the loop:

for (Portfolio *p in portfolios) { NSLog(@"The value of portfolio %d is $%.2f which has the following stocks:" , [p portfolioID], [p portfolioValue]); NSMutableArray *stocks = [p portfolio]; for (StocksHolding *s in stocks) { NSLog(@"Name: %@ ", [s name]); } }

Works the way I want now!
2012-02-07 21:16:43.599 Stocks[4973:707] The value of portfolio 1 is $7705240.00 which has the following stocks:
2012-02-07 21:16:43.601 Stocks[4973:707] Name: APPL
2012-02-07 21:16:43.601 Stocks[4973:707] Name: e.on n
2012-02-07 21:16:43.602 Stocks[4973:707] Name: GOOG
2012-02-07 21:16:43.602 Stocks[4973:707] The value of portfolio 2 is $4601240.00 which has the following stocks:
2012-02-07 21:16:43.603 Stocks[4973:707] Name: e.on n
2012-02-07 21:16:43.603 Stocks[4973:707] Name: APPL

easy mistake… I’m coming from java so its all very similar but at the same time totally different. Really enjoying your book! Just trying to get my head around pointers…