objective-c 打印 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2216266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing an NSString
提问by ta.speot.is
What's the correct way to print an NSString in Objective-C? A lot of examples use NSLog(), but according to the documentation:
在 Objective-C 中打印 NSString 的正确方法是什么?许多示例使用 NSLog(),但根据文档:
NSLog is a FoundationKit function for printing debug statements to the console. ... NSLog works basically like: fprintf(stderr, format_string, args ...);
NSLog 是一个 FoundationKit 函数,用于将调试语句打印到控制台。... NSLog 的工作原理基本上类似于: 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:
这对我来说有点像 Win32/C++ 中的 _TRACE 宏。我不想打印到标准错误,我想打印到标准输出。有人建议使用 printf() 如下:
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.
但这似乎是间接打印 NSString 的额外级别,并且“感觉”不像解决方案。
采纳答案by kennytm
Well this isthe solution.
嗯,这就是解决方案。
Since printfis 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.
由于printf是纯 C 函数,它不会识别 Objective-C 对象。(NSLog 的格式化程序不同于 printf 的格式化程序。)因此,您必须在格式化之前将其转换为 C 字符串。
BTW you can use [str UTF8String]instead of [str cStringUsingEncoding:NSUTF8StringEncoding].
顺便说一句,您可以使用[str UTF8String]代替[str cStringUsingEncoding:NSUTF8StringEncoding].
回答by bbum
Spewing stuff to stdoutis 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.
stdout考虑到几乎所有项目本质上都是 GUI,因此在 Cocoa 中喷出东西实际上是一件非常罕见的事情。作为命令行工具构建或以其他方式需要处理的项目相对较少stdout。
However, the Foundation does provide the means to write to stdout. Specifically, NSFileHandlehas fileHandleWithStandardOutputwhich gives you a file handle that can write to stdout.
但是,基金会确实提供了写入标准输出的方法。具体来说,NSFileHandlehasfileHandleWithStandardOutput为您提供了一个可以写入stdout.
From there, it is a matter of converting the NSStringto an NSDataand writing it.
从那里开始,将 转换NSString为 anNSData并编写它。
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];
}
回答by Jayce
C string (UTF8String) is a pointer to a structure inside the string object.
C 字符串(UTF8String)是指向字符串对象内部结构的指针。
NSString *str = @"Hello, World.";
printf("%s\n", [str UTF8String]);
回答by svth
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
}
回答by Eimantas
You should use the custom file handler or write a macro yourself.
您应该使用自定义文件处理程序或自己编写宏。
Tip: when NSLog prints out an object it uses object's debugDescriptionmethod. You could override this method for your custom NSObjectsubclasses to print custom debugInfo to stdout.
提示:当 NSLog 打印出一个对象时,它使用对象的debugDescription方法。您可以为您的自定义NSObject子类覆盖此方法以将自定义调试信息打印到标准输出。
回答by LavaSlider
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 );

