Spewing stuff to stdout
is actually a pretty rare thing to do in Cocoa, given that almost all projects are GUI in nature. There are relatively few projects that are built as command line tools or otherwise need to deal with stdout
.
However, the Foundation does provide the means to write to stdout. Specifically, NSFileHandle
has fileHandleWithStandardOutput
which gives you a file handle that can write to stdout
.
From there, it is a matter of converting the NSString
to an NSData
and writing it.
Quite a few steps, but easily wrapped up in a reusable function:
void MyLog(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *formattedString = [[NSString alloc] initWithFormat: format
arguments: args];
va_end(args);
[[NSFileHandle fileHandleWithStandardOutput]
writeData: [formattedString dataUsingEncoding: NSNEXTSTEPStringEncoding]];
[formattedString release];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…