Reputation: 263
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
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
Reputation: 150565
NSLog is like a printf, but it does a bit more:
@
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).Upvotes: 26
Reputation: 69027
I see two main differences between NSLog
and printf
:
NSLog
supports NSString
objects through the %@
extension;
furthermore, NSLog
automatically adds time and process data (e.g., 2012-01-25 17:52:10.479 process[906:707])
Upvotes: 3
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