Swift equivalent of Java toString
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, every class inherits toString() from Object, which returns a string representation of the instance. In Swift, the equivalent is the description property provided by the CustomStringConvertible protocol. Conforming to this protocol lets you control what print(), string interpolation, and debugger output show for your types. Swift also provides CustomDebugStringConvertible for debug-specific representations and String(describing:) as a universal conversion function.
Java toString vs Swift description
In Java, toString() is inherited from Object — every class has it. In Swift, CustomStringConvertible is opt-in. Without it, print() uses a default representation that shows the type name and stored properties.
Default Behavior Without CustomStringConvertible
Swift structs get a reasonable default output. Classes print their type and memory address without CustomStringConvertible.
CustomDebugStringConvertible
Use CustomStringConvertible for user-facing output and CustomDebugStringConvertible for developer-facing debug output. This mirrors Java's toString() (user) vs debugger views.
String Conversion Methods
Enums and CustomStringConvertible
Using with Collections
Common Pitfalls
- Forgetting that CustomStringConvertible is opt-in: Unlike Java where every class inherits
toString(), Swift requires explicit protocol conformance. Without it, classes show their type and memory address (e.g.,MyClass 0x600000123abc). Structs get a better default showing stored property values. - Implementing description instead of debugDescription for debugging:
print()usesdescription. The LLDBpocommand anddebugPrint()usedebugDescription. If you only implementdescription,debugPrint()falls back to it, but if you want richer output in the debugger, implementCustomDebugStringConvertibleseparately. - Using String(describing:) on optionals:
String(describing: someOptional)produces"Optional(value)"or"nil", not just the unwrapped value. Unwrap first or usesomeOptional.map { String(describing: $0) } ?? "nil". - Expensive computation in description: The
descriptionproperty is called every time the object is printed, logged, or interpolated into a string. Avoid heavy computation, database queries, or network calls in this property — keep it simple and fast. - Confusing String() initializer with String(describing:):
String(42)works for types that conform toLosslessStringConvertible(Int, Double, Bool). For custom types, useString(describing: myObject)or conform toCustomStringConvertible.
Summary
- Swift's
CustomStringConvertibleprotocol withdescriptionis the equivalent of Java'stoString() CustomDebugStringConvertiblewithdebugDescriptionprovides a separate debug representation- Swift structs get a default description showing stored properties; classes show type and address
- Use
String(describing:)for explicit conversion, string interpolation callsdescriptionautomatically - Unlike Java,
CustomStringConvertibleis opt-in — you must explicitly conform to the protocol
Related reading
- Swift equivalent of Java toString
- Swing vs JavaFx for desktop applications
- Switch on Enum in Java
- Switch statement for matching substrings of a String
- Swift equivalent of NSBundle bundleForClassself class
- Swift extract regex matches
- Synchronization of non-final field
- Synchronizing access to SimpleDateFormat

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the 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.