Skip to content

Instantly share code, notes, and snippets.

@hansott
Last active March 18, 2019 02:46
Show Gist options
  • Save hansott/887b1561999e95532f9e to your computer and use it in GitHub Desktop.
Save hansott/887b1561999e95532f9e to your computer and use it in GitHub Desktop.
Swift toString() method
// Searching for a toString() method in Swift? Use the printable protocol!
// Implement Printable protocol and create a description variable
// https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Printable.html
class Person : Printable {
var name : String!
init(name : String) {
self.name = name
}
var description : String {
return "name=\(name)"
}
}
// Usage
var myself : Person = Person(name: "Hans Ott")
println(myself) // name=Hans Ott
@A86S
Copy link

A86S commented Jan 3, 2018

Thanks !!!

@cuimingda
Copy link

printable has been renamed to CustomStringConvertible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment