Difference between NSLog and Printf statement for ObjectiveC

24,132

Solution 1

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

  • A timestamp is added to the output.
  • The output is sent to the Xcode console, or whatever stderr is defined as.
  • 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).
  • 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”.

Solution 2

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.

Solution 3

I see two main differences between NSLog and printf:

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

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

Share:
24,132
Rushi3311
Author by

Rushi3311

Updated on July 21, 2020

Comments

  • Rushi3311
    Rushi3311 almost 4 years

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

    Why do all developer use NSLog instead of Printf ?

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

    At which point can they be differentiated ?

  • James Webster
    James Webster over 10 years
    More accurately: NSLog supports objects through the %@ extension, by implicitly calling that object's -(NSString*) description; method
  • Munim
    Munim about 9 years
    The third biggest difference is that NSLog sends output to stderr, where as printf sends to stdout.
  • jxramos
    jxramos over 5 years
    Question, is this Apple System Log synonymous with Unified Logging which appears to use the os_log construct.
  • jxramos
    jxramos over 5 years