NSDictionary `description` formatting problem — treats structure like char data

后端 未结 2 423
谎友^
谎友^ 2020-12-18 00:34

I\'ve got a custom class (which resembles an NSArray in concept and, hopefully, formatted appearance) which has a description formatter.

When the outp

相关标签:
2条回答
  • 2020-12-18 01:16

    There's no real answer to this question (the "security feature" of OS X doesn't appear to affect iOS console writes), but there are these two work-arounds:

    #1: Interestingly, if I make my class a subclass of NSArray then NSDictionary calls descriptionWithLocale:indent: and it formats correctly. Sounds like NSDictionary is "cheating" and testing isKindOfClass rather than respondsToSelector, or else is just prejudiced against non-NS stuff.

    It's kind of ugly to have to subclass NSArray, though, in terms of acquiring a lot of behaviors I don't want to mimic, and carrying extra unused data. Etc

    #2: Another option is to convert the escaped string back to its original. This takes a 31-line procedure to handle the basics (\n, \t, \", and \). The up-side is that I don't need to subclass NSArray. The main downside is that this routine must be inserted in any NSLog call that could display my class. Another minor downside is that the escaped strings were wrappered with quote characters I can't eliminate, but that's hardly noticeable.

    (Accepted this answer, even though it's not the "real" answer, because my accepted % would suffer otherwise. I ask too many difficult questions, I guess.)

    0 讨论(0)
  • 2020-12-18 01:23

    This is a wonderful security-related "feature" that was introduced in OS X 10.5+ version of syslog().

    As explained by an Apple engineer in this post: Who broke NSLog on Leopard?,

    That's the behavior of syslog(). From the man page:

    Newlines and other non-printable characters embedded in the message string are printed in an alternate format. This prevents someone from using non-printable characters to construct misleading log messages in an output file. Newlines are printed as "\n", tabs are printed as "\t". Other control characters are printed using a caret ("^") representation, for example "^M" for carriage return.

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

    Chris Kane Cocoa Frameworks, Apple

    See man syslog for more info.

    0 讨论(0)
提交回复
热议问题