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
Swift does not have a method named toString() in the Java sense. The closest equivalent is conforming a type to CustomStringConvertible and implementing the description property.
The Closest Equivalent: CustomStringConvertible
In Java, you override toString() so printing an object gives a useful representation. In Swift, you usually conform to CustomStringConvertible.
Both print(user) and String(describing: user) use the description value.
Why This Is Not Literally toString()
Swift relies more on protocols and generic string conversion than on a universal base-class override point. That means the concept exists, but the mechanism is more protocol-oriented.
A rough comparison is:
- Java
toString()maps to Swiftdescription - Java
System.out.println(obj)maps to Swiftprint(obj) - Java debug-style object output maps to Swift
debugPrint(obj)orString(reflecting: obj)
That is the right mental model when moving between the languages.
Debug Output: CustomDebugStringConvertible
If you want a more detailed representation for debugging, use CustomDebugStringConvertible as well.
Use description for normal output and debugDescription for developer-oriented detail.
Classes, Structs, And Enums
The pattern works across Swift types.
For a class:
For an enum:
The key idea is always the same: define the string representation through a protocol conformance.
String Interpolation Uses The Same Idea
Swift string interpolation also uses the object's string representation.
That is one reason CustomStringConvertible feels like Swift's version of toString(): the representation flows naturally into printing and interpolation.
Default Output Versus Custom Output
Some Swift types already print reasonably well without custom code, especially structs with simple stored properties. But that default output is not a stable contract and may not match what you want users or logs to see.
If the textual form matters, define it explicitly.
That is especially true for:
- user-facing messages
- log lines you want to keep readable
- domain objects where only some fields matter
- security-sensitive types where you should not print everything
Common Pitfalls
The most common mistake is looking for a toString() override and missing the protocol-based design. In Swift, the right place is description, not a method with the Java name.
Another mistake is using description for expensive work. Printing can happen often, so keep the representation cheap and deterministic.
Developers also confuse normal and debug output. print uses description, while debugPrint and String(reflecting:) prefer the debug form.
Finally, be deliberate about what you expose. A good string representation should help readers, not dump every internal field by default.
Summary
- Swift's closest equivalent to Java's
toString()isCustomStringConvertiblewithdescription. - '
print(obj)andString(describing: obj)use that representation.' - Use
CustomDebugStringConvertiblefor debug-focused output. - The pattern works for structs, classes, and enums.
- Think of Swift string conversion as protocol-based rather than base-class-based.
Related reading
- Swing vs JavaFx for desktop applications
- Switch on Enum in Java
- Switch statement for matching substrings of a String
- Synchronization of non-final field
- Swift equivalent of NSBundle bundleForClassself class
- Swift extract regex matches
- Synchronizing access to SimpleDateFormat
- Syncing multiple asynchronous requests in Java

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.