objective-c 目标 C 通用打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2046749/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:44:38  来源:igfitidea点击:

Objective C General Print

objective-c

提问by Casebash

Does objective C have a general print command like Python? NSLog seems to log it rather than print out to console. printf only accepts specific types.

目标 C 是否有像 Python 这样的通用打印命令?NSLog 似乎记录它而不是打印到控制台。printf 只接受特定类型。

回答by phoebus

NSLog()does print to the console, and is very similar to C's printf(). Having its origins and basis in C, console printing is done as it is in C, essentially.

NSLog()确实打印到控制台,并且非常类似于 C 的printf(). 控制台打印的起源和基础是在 C 中完成的,本质上是在 C 中完成的。

回答by Dave DeLong

printfis what you're looking for. You can use it like a regular print statement:

printf就是你要找的。您可以像使用常规打印语句一样使用它:

printf("This is a neat command!\n");

You're also probably aware that you can use it with substitutions:

您可能还知道可以将其与替换一起使用:

printf("The Answer is %d\n", 42);

回答by Barry Wark

You can use NSStringto format strings containing id types as well as the standard printf types, then just print it using printf:

您可以使用NSString格式化包含 id 类型以及标准 printf 类型的字符串,然后使用 printf 打印它:

NSString *fmt = [NSString stringWithFormat:@"My formatted string: %@", anObject];

printf("%s", [fmt cStringUsingEncoding:[NSString defaultCStringEncoding]]);