I have a custom class (which is similar to NSArray in concept and hopefully a formatted look) that has a description formatter.
When formatting output (NSLog) is printed on its own, it looks great, but when it is included as an NSDictionary description element, NSDictionary formatting seems to resolve it as a character string and not a structure definition, enclose it in quotation marks and remove all control characters in line.
This is not the case for the standard NSArray, of course, so I wonder how it decides to process the string one by one.
For example, instead of output that looks like this:
theChildren = ( { "@meta.type" = "ns3:location"; "childNumber" = 0; ...
Looks like:
theChildren = "( \n\t\t {\n\t \"@meta.type\" = \"ns3:location\";\n\t \"childNumber\" = 0; ...
Can anyone suggest a way to change this behavior?
FWIW, I am accumulating a description string (with data consisting mainly of the results of calls to NSDictionary description ) in an NSMutableString, and then an NSString stringFromString in the output (although I don't know if this does anything).
Added
I think I found the answer (not tested yet) buried in the NSDictionary entry:
Discussion
The returned NSString object contains string representations of each of the dictionary entries. descriptionWithLocale: Indent: Gets a string representation of the specified key or value as follows:
If the object is an NSString object, it is used as is. If the object responds to descriptionWithLocale:indent:, that
To get a string representation of objects, the method is called..
If the object responds to descriptionWithLocale:, that method is
to get a representation of the string of objects.
If none of the above conditions is met, the object's string
a representation is obtained by calling its description method.
Update
Well, it turns out they are lying. I implemented a description of WhithLocale: indent: and it is never called.
Update 9/23
Interestingly, if I make my class a subclass of NSArray, then the NSDictionary calls descriptionWithLocale: indent: and it formats correctly. It seems that NSDictionary is a "trick" and testing isKindOfClass , not a respondsToSelector , or it is simply biased against things other than NS.
It seems to be ugly to have a subclass of NSArray, although from the point of view of acquiring a large number of actions, I do not want to simulate and carry unnecessary unused data.
Etc
Another option is to convert the escape string back to the original. This requires a 31-line procedure to handle the basics ( \n , \t , \" and \\ ). At the top is that I do not need to subclass NSArray. The main disadvantage is that this procedure must be inserted into any calling NSLog, which my class can display.Another minor flaw is that the escaped strings were wrapped with quote characters, which I cannot eliminate, but this is barely noticeable.
Got this #ifdefed at the moment - not sure what I will choose.