如何在NSLog中摆脱所有这些垃圾?

2 iphone cocoa cocoa-touch objective-c uikit

我用的时候

NSLog(@"fooBar")
Run Code Online (Sandbox Code Playgroud)

它打印出很多我不想要的东西:

2009-09-03 13:46:34.531 MyApp[3703:20b] fooBar
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有这个大前缀的情况下将某些内容打印到控制台?我想在控制台中画一张桌子和其他东西,这样空间才是至关重要的......

小智 9

这是来自borkware.com的Mark Dalrymple

http://borkware.com/quickies/single?id=261

更安静的NSLog(通用 - >黑客)[永久链接]

// NSLog() writes out entirely too much stuff.  Most of the time I'm
// not interested in the program name, process ID, and current time
// down to the subsecond level.
// This takes an NSString with printf-style format, and outputs it.
// regular old printf can't be used instead because it doesn't
// support the '%@' format option.

void QuietLog (NSString *format, ...)
{
  va_list argList;
  va_start (argList, format);
  NSString *message = [[[NSString alloc] initWithFormat: format
                                              arguments: argList] autorelease];
  printf ("%s", [message UTF8String]);
  va_end  (argList);

} // QuietLog
Run Code Online (Sandbox Code Playgroud)

  • 那会起作用......但printf("%s",......)效率低下.如果你把它称为一堆(最后发布消息),你可能想要避免自动释放.请参阅:http://stackoverflow.com/questions/1354728/in-xcode-is-there-a-way-to-disable-the-timestamps-that-appear-in-the-debugger-co/1354736#1354736 (2认同)