Given the following test code, why aren't the string interpolations \(t1) and \(t2) working? Renaming description
causes the compiler to fail with "Type 'Test' does not conform to protocol 'Printable'" and description
obviously works given the second println of each test.
enum Test: Printable {
case A
case B(Int)
var description: String {
switch self {
case .A:
return ".A"
case let .B(value):
return ".B: value = \(value)"
}
}
}
let t1 = Test.A
let t2 = Test.B(-100)
println("t1 = \(t1)") // prints "t1 = (Enum Value)"
println(t1.description) // prints ".A"
println("t2 = \(t2)") // prints "t2 = (Enum Value)"
println(t2.description) // prints ".B: value = -100"
Note that enums being printed as (Enum Value)
is a known bug in Swift version 1.0 (swift-600.0.51.3). It will be fixed in a future release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With