-3

I have the code written already - but I need some clarification that I couldn't find in some other answers on this site. I couldn't find a solid example to help me.

Is this correctly considered 'iterating' through my array? I feel like this really hard coded. I'm learning still. Thanks

    NSMutableArray *stocks = [NSMutableArray array];


    BNRStockHolding *A = [[BNRStockHolding alloc]init];
    BNRStockHolding *B = [[BNRStockHolding alloc]init];
    BNRStockHolding *C = [[BNRStockHolding alloc]init];


    [stocks insertObject:A atIndex:0];
    [stocks insertObject:B atIndex:1];
    [stocks insertObject:C atIndex:2];

    for (int i = 0; i < 1; i++)
    {

    {
    [A setNumberOfShares:40];
    [B setNumberOfShares:90];
    [C setNumberOfShares:210];

    [A setPurchaseSharePrice:2.30];
    [B setPurchaseSharePrice:12.19];
    [C setPurchaseSharePrice:45.10];

    [A setCurrentSharePrice:4.50];
    [B setCurrentSharePrice:10.56];
    [C setCurrentSharePrice:49.51];

    float costA = [A costInDollars];
    float costB = [B costInDollars];
    float costC = [C costInDollars];
    NSLog(@"This stock costs %.2f", costA);
    NSLog(@"This stock costs %.2f", costB);
    NSLog(@"This stock costs %.2f", costC);

    NSLog(@"\n");

    float valueA = [A valueInDollars];
    float valueB = [B valueInDollars];
    float valueC = [C valueInDollars];
    NSLog(@"The current value of this stock is %.2f", valueA);
    NSLog(@"The current value of this stock is %.2f", valueB);
    NSLog(@"The current value of this stock is %.2f", valueC);
    }
    }
Apetro
  • 63
  • 7

1 Answers1

1

Your current for loop isn't actually looping through anything. If you want to iterate through the array to print out each value, here's what you do:

for(BNRStockHolding *stockHolding in stocks) {

    NSLog(@"This stock costs: %.2f", [stockHolding costInDollars]);
    NSLog(@"The current value of this stock is %.2f", [stockHolding valueInDollars]);
}

This really is a basic concept. I would read through this answer regarding for loop fundamentals (and maybe review some other fundamentals) before attempting to write full blown apps.

Community
  • 1
  • 1
pasawaya
  • 11,515
  • 7
  • 53
  • 92