How to print Boolean flag in NSLog?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Objective-C, a Boolean value can be logged with NSLog, but the best format depends on whether you want numeric output or readable YES and NO text. For debugging, the most useful pattern is usually a ternary expression that prints a clear string rather than raw 0 or 1.
Print a BOOL Numerically with %d
A BOOL in Objective-C is commonly treated like a small integer, so %d works.
This prints 1 for YES and 0 for NO. It is technically correct, but it is not always the easiest form to scan in logs.
Print YES or NO for Better Readability
For most debugging output, a readable string is better than a numeric flag.
That makes the meaning obvious without requiring you to remember what the numeric value represents.
Log C99 bool Values Too
If you are using the C99 bool type instead of Objective-C BOOL, the same %d approach works because the runtime value is still effectively numeric.
If readability matters, you can still use a ternary string expression.
Use a Macro for Repeated Flag Logging
If you log Boolean flags often, a small macro keeps the output consistent.
This logs both the variable name and its readable value. For quick debugging sessions, that is often more useful than repeatedly writing one-off NSLog statements.
Keep the Log Output Intentional
Logging Booleans looks trivial, but the real question is what kind of debugging signal you want. Numeric output is compact. Text output is clearer. A logging helper macro is best when repeated flags appear in the same subsystem.
The key is consistency. If one part of the code prints 1 and another prints YES, log scanning becomes harder than it needs to be.
Remember That NSLog Is Debug-Oriented
NSLog is fine for development diagnostics, but production logging often needs a more structured approach. If the Boolean represents meaningful state in a shipping system, you may want a real logging framework, subsystem tags, or unified logging instead of scattering raw NSLog calls everywhere.
That does not change how to print the value, but it does change where and why you print it.
One extra benefit of the macro approach is that it keeps the formatting decision in one place. If the team later wants true and false instead of YES and NO, you can change one definition instead of every call site.
Common Pitfalls
- Using
%@directly with aBOOLinstead of converting it to an Objective-C object or string first. - Printing only
0and1when the logs would be easier to read asYESandNO. - Mixing
BOOLandboolwithout understanding that the logging intent is readability, not just type compatibility. - Repeating ad hoc Boolean log formatting throughout the codebase instead of centralizing it.
- Leaving temporary
NSLogflag statements in production paths where structured logging would be more appropriate.
Summary
- '
%dprints a Boolean numerically as0or1.' - A ternary expression with
%@is better for readableYESandNOoutput. - The same basic approach works for both
BOOLand C99bool. - A small macro can standardize repeated Boolean logging.
- Prefer clear and consistent log output over the shortest possible format.
Related reading
- How to print call stack in Swift?
- How to print details of a 'catch all' exception in Swift?
- How to print to the console in Android Studio?
- How to print to the console in Android Studio?
- How to print the current Stack Trace in .NET without any exception?
- How to print the value of a Tensor object in TensorFlow?
- How to print to the Xcode console in SwiftUI
- How to program a delay in Swift 3
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.