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'sdescription
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
:
NSLog
supportsNSString
objects through the%@
extension;furthermore,
NSLog
automatically adds time and process data (e.g., 2012-01-25 17:52:10.479 process[906:707])
Author by
Rushi3311
Updated on July 21, 2020Comments
-
Rushi3311 almost 4 years
I want to know about the difference between the
NSLog
and thePrintf
statement in Objective-C (for application purpose...!)Why do all developer use
NSLog
instead ofPrintf
?Both look similar, but what is the difference in internal working?
At which point can they be differentiated ?
-
James Webster over 10 yearsMore accurately:
NSLog supports objects through the %@ extension, by implicitly calling that object's -(NSString*) description; method
-
Munim about 9 yearsThe third biggest difference is that NSLog sends output to stderr, where as printf sends to stdout.
-
jxramos over 5 yearsQuestion, is this Apple System Log synonymous with Unified Logging which appears to use the
os_log
construct. -
jxramos over 5 yearsApparently they're distinct as it turns out blog.safedk.com/technology/nslog-os_log-multiple-ios-versions. bignerdranch.com/blog/…