Skip to content

Instantly share code, notes, and snippets.

@marcinczenko
Last active February 11, 2016 11:47
Show Gist options
  • Save marcinczenko/f7d6dd3fbeb02dc1245d to your computer and use it in GitHub Desktop.
Save marcinczenko/f7d6dd3fbeb02dc1245d to your computer and use it in GitHub Desktop.
Playground demonstrates how can you properly add String conversion to your own classes. See http://blog.redgreenrefactor.eu/post/139107029424/string-conversions-in-swift.
//: Playground - noun: a place where people can play
class MyClass: CustomStringConvertible {
let counter: Int
var description: String {
return "{ counter: \(self.counter) }"
}
init(withCounter: Int) {
self.counter = withCounter
}
}
//extension String {
// init(_ myClass: MyClass) {
// self = "{ counter: \(myClass.counter) }"
// }
//}
let str = String(MyClass(withCounter: 25))
let str2 = "\(MyClass(withCounter:25))"
let str3 = "\(String(MyClass(withCounter:25)))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment