Answer to Challenge


#1

Key-concept to answer challenge 20 is about “%@” or description…

by overriding description method on Base and Sub-Class we can easily fast enumerating through array, which each object can describe itself.

BNRStockHolding.m

[code]-(NSString *)description{

NSString *objectDescriptionFormat = @"\n" "========= Properties of Stock Value %@ ==========\n"
"Purchase price is %f\n" "Current Price is %f\n" "Number of Shares bought is %i\n" "\n"
"------------------- Total Value & Cost ------------------\n"
"Value in dollar for this stock is %f\n" "Cost in dollar for this stock is %f\n" "\n";

return [NSString stringWithFormat:objectDescriptionFormat, [super description],self.purchaseSharePrice,self.currentSharePrice,self.numberOfShares,self.valueInDollars,self.costInDollars];

}

BNRForeignStockHolding.m

-(NSString *)description
{
NSString *objectDescriptionFormat = @"%@" “Conversion rate for this stock is %f\n” “\n” ;
return [NSString stringWithFormat: objectDescriptionFormat,[super description],self.conversionRate];
}
[/code]

then by using Fast Enumeration method you only need to put descriptor sign.

Note: This challenge is really helpful, it helps newbie like us to explore more (especially about overriding description method), before this, I tried to fast enumeration with method to check if object inside array is StockHolding Class or sub class of stockholding (ForeignStock), then perform different NSLog to treat each object. it’s really code smell. :laughing: