What is the Swift equivalent of Java toString() to print the state of a class instance?
Swift equivalent of Java toString()
57.3k Views Asked by Marcus Leon AtThere are 4 best solutions below
On
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.
On
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))
On
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.
The
descriptionproperty is what you are looking for. This is the property that is accessed when you print a variable containing an object.You can add
descriptionto your own classes by adopting the protocolCustomStringConvertibleand then implementing thedescriptionproperty.descriptionis also used when you call theStringconstructor:This is the recommended method for accessing the instance description (as opposed to
myobj.descriptionwhich will not work if a class doesn't implementCustomStringConvertible)