How to print just once?


#1

So I have all my code below from the main.m that I wrote, and it works pretty good except for one major problem. When I iterate through the array using for it does so 3 times, because I am using the allMyStocks array as the base (and it obviously contains 3 stocks). How do I get it to just run through one time?

StockHolding *stock1 = [[StockHolding alloc] init];
StockHolding *stock2 = [[StockHolding alloc] init];
StockHolding *stock3 = [[StockHolding alloc] init];

NSMutableArray *allMyStocks = [NSMutableArray arrayWithObjects:stock1, stock2, stock3, nil];

[stock1 setPurchaseSharePrice:2.30];
[stock1 setNumberOfShares:40];
[stock1 setCurrentSharePrice:4.50];

[stock2 setPurchaseSharePrice:12.19];
[stock2 setNumberOfShares:90];
[stock2 setCurrentSharePrice:10.56];

[stock3 setPurchaseSharePrice:45.10];
[stock3 setNumberOfShares:210];
[stock3 setCurrentSharePrice:49.51];

for(NSString *stocks in allMyStocks){
    NSLog(@"Stock 1 cost $%.2f and is now worth $%.2f. Stock 2 cost $%.2f and is now worth $%.2f. Stock 3 cost $%.2f and is now worth $%.2f.", [stock1 costInDollars], [stock1 valueInDollars], [stock2 costInDollars], [stock2 valueInDollars], [stock3 costInDollars], [stock3 valueInDollars]);
}

Thanks so much guys. Loving the book, have it on Kindle on my Mac with the dual screens. Book on the right, xcode on the left, the ultimate learning station :slight_smile:


#2

First, your loop variable should be a StockHolding *, not an NSString *

for (StockHolding *stock in allMyStocks)
But you don’t really need that loop if you follow the rest of this.

I would probably implement a description method in StockHolding.m:

- (NSString *)description { return [NSString stringWithFormat:@"Stock: cost $%.2f and is now worth $%.2f", [self costInDollars], [self valueInDollars]]; }

Then just print the array:


#3

That opens up a whole new world to me, thats pretty awesome, but heres the one thing I am now confused about (it all works btw).

How does the printed array know to use the description method that I wrote? I mean its using it, and its working, but I don’t see how it knows to use it.

I must be missing something.

EDIT: I just noticed that it prints it 3 times again.

for (StockHolding *stock in allMyStocks)
{
    NSLog(@"array = %@", allMyStocks);
}

Prints:

2011-10-20 03:48:14.376 Stocks[17561:903] array = (
“Stock: cost 92.00 and is now worth 180.00”,
“Stock: cost 1097.10 and is now worth 950.40”,
“Stock: cost 9471.00 and is now worth 10397.10”
)
2011-10-20 03:48:14.380 Stocks[17561:903] array = (
“Stock: cost 92.00 and is now worth 180.00”,
“Stock: cost 1097.10 and is now worth 950.40”,
“Stock: cost 9471.00 and is now worth 10397.10”
)
2011-10-20 03:48:14.381 Stocks[17561:903] array = (
“Stock: cost 92.00 and is now worth 180.00”,
“Stock: cost 1097.10 and is now worth 950.40”,
“Stock: cost 9471.00 and is now worth 10397.10”
)


#4

Every object has a description method. It is defined in NSObject as ClassName:HexAddress, but you have overridden it.

So has NSArray. NSArray’s description method calls the description of every object in the array.


#5

Oh, and get rid of the loop and it won’t print it three times.


#6

Hi fotes50!

for(NSString *stocks in allMyStocks){ NSLog(@"Stock 1 cost $%.2f and is now worth $%.2f. Stock 2 cost $%.2f and is now worth $%.2f. Stock 3 cost $%.2f and is now worth $%.2f.", [stock1 costInDollars], [stock1 valueInDollars], [stock2 costInDollars], [stock2 valueInDollars], [stock3 costInDollars], [stock3 valueInDollars]); }
May be it would be a good idea to read the chapter about forin-loops (fast enumeration) again before you go on with the "description-solution"
It seems that you have not understood yet how this loop works - the apple documentation is always a good source too.

cu
Vertex