Formatting Problem NSDictionary `description` - considers a structure similar to char data

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.

+8
ios formatting objective-c iphone
source share
2 answers

There is no real answer to this question (the OS X security feature does not seem to affect iOS console entries), but there are two points:

# 1: Interestingly, if I make my class a subclass of NSArray, then NSDictionary calls the descriptionWithLocale: indent: and it formats correctly. It seems that NSDictionary is a β€œtrick” and testing of isKindOfClass, not a response to ToSelector, or just biased against non-NS material.

No matter how ugly the NSArray subclass was, 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

# 2: Another option is to convert the escape string back to the original. It takes a 31-line procedure to handle the basics (\ n, \ t, \ "and \). The upside is that I don't need to subclass NSArray. The main disadvantage is that this procedure must be inserted in any call NSLog, which my class can display.Another minor flaw is that the escaped strings were wrapped in quotation marks, which I cannot eliminate, but this is barely noticeable.

(Accepted this answer, although this is not a β€œreal” answer, because my accepted% will suffer differently. I ask too many difficult questions, I think.)

+2
source share

This is a great security feature that was introduced in OS X 10.5+ syslog() .

As an Apple engineer explained in this post: Who violated the NSLog on Leopard? ,

This is the behavior of syslog() . On the man page:

New lines and other non-printing characters embedded in the message line is printed in an alternative format. This prevents the use of non-printable characters to create a misleading message log in the output file. New lines are printed as "\ n", tabs are printed as "\ T". Other control characters are printed using a carriage ("^") representation, such as "^ M" to return a carriage.

The ASL subsystem that NSLog () writes does the same thing (at least in Leopard). Writing XML to a file is a reasonable alternative.

Chris Kane Cocoa Framework, Apple

See man syslog more details.

+5
source share

All Articles