ObjectiveC的NSLog和Printf语句之间的区别

时间:2021-12-16 19:57:51

I want to know about the difference between the NSLog and the Printf statement in Objective-C (for application purpose...!)

我想知道Objective-C中NSLog和Printf语句之间的区别(用于应用程序......!)

Why do all developer use NSLog instead of Printf ?

为什么所有开发人员都使用NSLog而不是Printf?

Both look similar, but what is the difference in internal working?

两者看起来相似,但内部工作有什么不同?

At which point can they be differentiated ?

他们可以在哪个方面加以区分?

4 个解决方案

#1


29  

  • printf() is a C standard library function, accepting a C string constant (const char *) as its format argument. printf() writes to stdout.

    printf()是一个C标准库函数,接受一个C字符串常量(const char *)作为其格式参数。 printf()写入stdout。

  • NSLog() is a Foundation function, accepting a constant NSString as format, and has an extended format specifier set (for example, printf() does't print objects specified by %@, NSLog() does). NSLog() also prints the process name and date before it prints the actual format and writes to sdterr.

    NSLog()是一个Foundation函数,接受一个常量NSString作为格式,并具有一个扩展格式说明符集(例如,printf()不打印由%@,NSLog()指定的对象)。 NSLog()还会在打印实际格式之前打印进程名称和日期,并写入sdterr。

Basically, we can say that NSLog() is an extended printf() Style function for Objective-C (more precisely, Cocoa and Cocoa Touch) and specific purposes.

基本上,我们可以说NSLog()是Objective-C(更确切地说,Cocoa和Cocoa Touch)和特定用途的扩展pr​​intf()Style函数。

#2


21  

NSLog is like a printf, but it does a bit more:

NSLog就像一个printf,但它做得更多:

  • A timestamp is added to the output.
  • 时间戳添加到输出中。
  • The output is sent to the Xcode console, or whatever stderr is defined as.
  • 输出发送到Xcode控制台,或者定义为stderr的任何内容。
  • It accepts all the printf specifiers, but it also accepts the @ operator for objects which displays the string provided by the object's description method. (description is part of NSObject, so all objects can override it to return a string that describes the object).
  • 它接受所有printf说明符,但它也接受@运算符用于显示对象描述方法提供的字符串的对象。 (描述是NSObject的一部分,因此所有对象都可以覆盖它以返回描述对象的字符串)。
  • The output is also sent to the Apple System Log (ASL), which is Apple's version of syslogd. This data can be read by other applications using a C API, or by a OS X user using the application “Console”.
  • 输出也会发送到Apple系统日志(ASL),这是Apple的syslogd版本。其他应用程序可以使用C API或使用应用程序“Console”的OS X用户读取此数据。

#3


8  

From a developer point of view, the biggest difference is that NSLog supports Objective-C object types via the %@ format. NSLog also writes to stderr, while printf writes to stdout.

从开发人员的角度来看,最大的区别是NSLog通过%@格式支持Objective-C对象类型。 NSLog也写入stderr,而printf写入stdout。

#4


3  

I see two main differences between NSLog and printf:

我看到NSLog和printf之间有两个主要区别:

  1. NSLog supports NSString objects through the %@ extension;

    NSLog通过%@扩展支持NSString对象;

  2. furthermore, NSLog automatically adds time and process data (e.g., 2012-01-25 17:52:10.479 process[906:707])

    此外,NSLog自动添加时间和过程数据(例如,2012-01-25 17:52:10.479过程[906:707])

#1


29  

  • printf() is a C standard library function, accepting a C string constant (const char *) as its format argument. printf() writes to stdout.

    printf()是一个C标准库函数,接受一个C字符串常量(const char *)作为其格式参数。 printf()写入stdout。

  • NSLog() is a Foundation function, accepting a constant NSString as format, and has an extended format specifier set (for example, printf() does't print objects specified by %@, NSLog() does). NSLog() also prints the process name and date before it prints the actual format and writes to sdterr.

    NSLog()是一个Foundation函数,接受一个常量NSString作为格式,并具有一个扩展格式说明符集(例如,printf()不打印由%@,NSLog()指定的对象)。 NSLog()还会在打印实际格式之前打印进程名称和日期,并写入sdterr。

Basically, we can say that NSLog() is an extended printf() Style function for Objective-C (more precisely, Cocoa and Cocoa Touch) and specific purposes.

基本上,我们可以说NSLog()是Objective-C(更确切地说,Cocoa和Cocoa Touch)和特定用途的扩展pr​​intf()Style函数。

#2


21  

NSLog is like a printf, but it does a bit more:

NSLog就像一个printf,但它做得更多:

  • A timestamp is added to the output.
  • 时间戳添加到输出中。
  • The output is sent to the Xcode console, or whatever stderr is defined as.
  • 输出发送到Xcode控制台,或者定义为stderr的任何内容。
  • It accepts all the printf specifiers, but it also accepts the @ operator for objects which displays the string provided by the object's description method. (description is part of NSObject, so all objects can override it to return a string that describes the object).
  • 它接受所有printf说明符,但它也接受@运算符用于显示对象描述方法提供的字符串的对象。 (描述是NSObject的一部分,因此所有对象都可以覆盖它以返回描述对象的字符串)。
  • The output is also sent to the Apple System Log (ASL), which is Apple's version of syslogd. This data can be read by other applications using a C API, or by a OS X user using the application “Console”.
  • 输出也会发送到Apple系统日志(ASL),这是Apple的syslogd版本。其他应用程序可以使用C API或使用应用程序“Console”的OS X用户读取此数据。

#3


8  

From a developer point of view, the biggest difference is that NSLog supports Objective-C object types via the %@ format. NSLog also writes to stderr, while printf writes to stdout.

从开发人员的角度来看,最大的区别是NSLog通过%@格式支持Objective-C对象类型。 NSLog也写入stderr,而printf写入stdout。

#4


3  

I see two main differences between NSLog and printf:

我看到NSLog和printf之间有两个主要区别:

  1. NSLog supports NSString objects through the %@ extension;

    NSLog通过%@扩展支持NSString对象;

  2. furthermore, NSLog automatically adds time and process data (e.g., 2012-01-25 17:52:10.479 process[906:707])

    此外,NSLog自动添加时间和过程数据(例如,2012-01-25 17:52:10.479过程[906:707])