What is the Swift equivalent of Java toString()
to print the state of a class instance?

- 7,983
- 3
- 57
- 80

- 55,199
- 118
- 297
- 429
-
More info @ http://stackoverflow.com/questions/24068506/how-can-i-change-the-textual-representation-displayed-for-a-type-in-swift – Marcus Leon Apr 13 '16 at 19:58
-
Does this answer your question? [How can I change the textual representation displayed for a type in Swift?](https://stackoverflow.com/questions/24068506/how-can-i-change-the-textual-representation-displayed-for-a-type-in-swift) – Top-Master Sep 22 '21 at 03:13
4 Answers
The description
property is what you are looking for. This is the property that is accessed when you print a variable containing an object.
You can add description
to your own classes by adopting the protocol CustomStringConvertible
and then implementing the description
property.
class MyClass: CustomStringConvertible {
var val = 17
public var description: String { return "MyClass: \(val)" }
}
let myobj = MyClass()
myobj.val = 12
print(myobj) // "MyClass: 12"
description
is also used when you call the String
constructor:
let str = String(myobj) // str == "MyClass: 12"
This is the recommended method for accessing the instance description (as opposed to myobj.description
which will not work if a class doesn't implement CustomStringConvertible
)
-
-
There's also `.debugDescription` for a more programmer-friendly version – nielsbot Apr 13 '16 at 01:57
-
3
-
I am following a tutorial of swift 2. They mentioned that `Printable` was the protocol to be used instead of `CustomStringConvertible `. `Printable` protocol was changed to `CustomStringConvertible ` in swift 3? – Antonio Apr 16 '17 at 19:50
-
1Yes, CustomStringConvertible replaced Printable. @Antonio, I don't remember when it changed, but it was certainly long before Swift 3. – vacawama Apr 16 '17 at 20:55
-
@Antonio, according to this article, the change happened at Swift 2. https://www.raywenderlich.com/108522/whats-new-in-swift-2 – vacawama Apr 16 '17 at 20:59
-
1
-
1
-
Is there a quick way to get a `description` for a `struct` with many attributes? `jvm` type languages tend to use reflection for this. – WestCoastProjects Jun 14 '20 at 23:01
-
@javadba, For a `struct`, if you print it `print(myStruct)` or use string interpolation `"\(myStruct)"`, you'll get a default value that might be sufficient for your needs. – vacawama Jun 15 '20 at 00:25
-
oh yea - I forgot that approach `let strOut = "\(myStruct)"` In the meantime I used `AppCode` to `Generate` the `description` – WestCoastProjects Jun 15 '20 at 00:45
If it is possible to use the struct instead of class, then nothing additional to do.
struct just prints fine itself to the output
print("\(yourStructInstance)")
or with class like this:
print(String(describing: yourClassInstance))

- 36,676
- 11
- 141
- 113
You should use String(obj)
.
Direct from the documentation for CustomStringConvertible:
NOTE
String(instance) will work for an instance of any type, returning its description if the instance happens to be CustomStringConvertible. Using CustomStringConvertible as a generic constraint, or accessing a conforming type's description directly, is therefore discouraged.

- 6,747
- 1
- 24
- 39
-
Seems like the `description()` method of `CustomStringConvertible` would be sufficient. – Greg Brown Oct 13 '16 at 13:13
How it's done with NSObject
extended classes
If your model class is extended from NSObject
, you have to override the Variable description
as follows:
public override var description: String {
return "\n{\n index: \(self.index),\n"
+ " country: \(self.name),\n"
+ " isoCountryCode: \(self.isoCountryCode),\n"
+ " localeId: \(self.localeId),\n"
+ " flagImageName: \(self.flagImageName!)\n}"
}
You can check how I have done it here within the Country
class, in the "CountryPicker iOS Swift library".
Or, to make it simpler for you to understand, your class and description
method should look like following:
public class MyClass: NSObject {
public var memberAttribute = "I'm an attribute"
public override var description: String {
return "My Class member: \(self.memberAttribute)"
}
}
Note:
Since you are extending your Modal class from NSObject
it doesn't require for your class to comply with CustomStringConvertible
class anymore, and you are overriding description
variable from the NSObject
class itself. Always remember, CustomStringConvertible
is mostly the pure Swift way of achieving this.

- 1
- 1

- 7,983
- 3
- 57
- 80