Last active
February 11, 2016 11:47
-
-
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.
This file contains 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
//: 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