Answer to Challenge


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.


[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];



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

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: