Last active
March 18, 2019 02:46
-
-
Save hansott/887b1561999e95532f9e to your computer and use it in GitHub Desktop.
Swift toString() method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
printable has been renamed to CustomStringConvertible
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks !!!