iphone - Correct implementation of NSObject description method for nested classes containing collections -


with own classes, override -(nsstring *)description method ease debugging. when invoking description on class implemented calls recursively description method other classes, formatting characters 'deeper' classes escaped. makes pretty printing difficult implement. here's example make clearer:

@interface foo {     nsarray *barsarray; } @end @implementation foo - (nsstring *)description {     return [nsstring stringwithformat: @"foo contents: %@", barsarray]; }  @interface bar {     nsstring *s1;     nsstring *s2; } @implementation bar - (nsstring *)description {     return [nsstring stringwithformat: @"s1: %@\ns2: %@", s1, s2]; } 

in case, \n newline characters description of class b escaped in output of class description method. idea how rid or circumvent behaviour? it's annoying when printing nested classes contain collections.

you can make use of nice formatting standard containers come with. example, bar description be:

- (id)containerdescription {   return [nsdictionary dictionarywithobjectsandkeys:s1, @"s1", s2, @"s2", nil]; }  - (nsstring *)description {   return [self.containerdescription description]; 

you can following on foo:

- (nsstring *)description {   nsarray *desc = [barsarray valueforkey:@"containerdescription"];   nsdictionary *descriptiondictionary =     [nsdictionary dictionarywithobjectsandkeys:desc, @"foo contents", nil];   return [descriptiondictionary description]; } 

the solution not optimal of course, have have second method , have call containerdescription on higher levels, it's 1 found yet.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -