Reputation: 27214
What's the correct way to print an NSString in Objective-C? A lot of examples use NSLog(), but according to the documentation:
NSLog is a FoundationKit function for printing debug statements to the console. ... NSLog works basically like: fprintf(stderr, format_string, args ...);
Which to me is a bit like the _TRACE macro in Win32/C++. I don't want to print to stderr, I want to print to stdout. There are people who suggest using printf() as follows:
printf("%s", [str cStringUsingEncoding:NSUTF8StringEncoding]);
But this seems like an extra level on indirection to get the NSString printed, and it doesn't "feel" like the solution.
Upvotes: 32
Views: 36777
Reputation: 2504
I just do:
define NSPrintf(...) printf( "%s", [[NSString stringWithFormat: __VA_ARGS__] UTF8String] )
Then i can use it as:
NSPrintf( @"Sorry %@, I can't do that\n", name );
Upvotes: 0
Reputation: 1310
I think you'll find these adequate for your needs:
// print to stdout
static void NSPrint(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
fprintf(stdout, "%s\n", [string UTF8String]);
#if !__has_feature(objc_arc)
[string release];
#endif
}
// print to stderr
static void NSPrintErr(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
fprintf(stderr, "%s\n", [string UTF8String]);
#if !__has_feature(objc_arc)
[string release];
#endif
}
Upvotes: 4
Reputation: 427
C string (UTF8String) is a pointer to a structure inside the string object.
NSString *str = @"Hello, World.";
printf("%s\n", [str UTF8String]);
Upvotes: 9
Reputation: 162712
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];
}
Upvotes: 29
Reputation: 49344
You should use the custom file handler or write a macro yourself.
Tip: when NSLog prints out an object it uses object's debugDescription
method. You could override this method for your custom NSObject
subclasses to print custom debugInfo to stdout.
Upvotes: 3
Reputation: 523214
Well this is the solution.
Since printf
is a pure C function, it won't recognize the Objective-C objects. (NSLog's formatter is distinct from printf's one.) Therefore, you have to convert it into a C string before formatting.
BTW you can use [str UTF8String]
instead of [str cStringUsingEncoding:NSUTF8StringEncoding]
.
Upvotes: 26