Rushi3311
Rushi3311

Reputation: 263

Difference between NSLog and Printf statement for ObjectiveC

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 ?

Upvotes: 22

Views: 25019

Answers (4)

user529758
user529758

Reputation:

  • printf() is a C standard library function, accepting a C string constant (const char *) as its format argument. printf() writes to 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.

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

Upvotes: 33

Abizern
Abizern

Reputation: 150565

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”.

Upvotes: 26

sergio
sergio

Reputation: 69027

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])

Upvotes: 3

Catfish_Man
Catfish_Man

Reputation: 41801

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.

Upvotes: 9

Related Questions