3

Consider this code in a Swift playground:

import Cocoa
class Thing: Printable {
    let name: String
    init() {
        name = "something"
    }
    var description: String { return name }
}

let a = Thing()
println("hello, \(a)")

As an image:

enter image description here

Expected: I expect the last line to print "hello, something".

Observed: it prints "hello, __lldb_expr_1.Thing" as if I had not implemented the Printable protocol.

The code actually works in the context of a real iOS app where it prints "hello, something" using the Printable protocol as expected.

Why does this code not print what I expect in the playground? Is this a limitation of playgrounds?

Jaanus
  • 17,688
  • 15
  • 65
  • 110
  • 1
    Yes, that seems to be a limitation of the playground, as noticed here http://stackoverflow.com/a/27830622/1187415: *"I find that both println and debugPrintln use description if-and-only-if the class descends from NSObject."* – Martin R Jan 18 '15 at 12:47
  • Interesting. Yes, it works in the Playground when I inherit from NSObject. But feels like it should also work without. I am following Session 404 “Advanced Swift” from WWDC 2014. It doesn’t talk specifically about Playgrounds but it does use this approach to log a Swift object that doesn’t inherit from NSObject. – Jaanus Jan 18 '15 at 12:51
  • 1
    Time to file another bug report :) – Martin R Jan 18 '15 at 12:52

1 Answers1

1

I think this bug have been fixed in Swift 1.2 swift-playground

Rafael Machado
  • 655
  • 6
  • 12