Difference between Printable and DebugPrintable in Swift

后端 未结 3 528
失恋的感觉
失恋的感觉 2021-01-11 12:33

Looking for a Swift equivalent of Cocoa\'s description, I found the following protocols in Swift: Printable and DebugPrintable.

<
3条回答
  •  一整个雨季
    2021-01-11 13:30

    In Xcode 6 Beta (Version 6.2 (6C101)) I find that both println and debugPrintln use description if-and-only-if the class descends from NSObject. I don't see that either uses debugDescription at all but when run in a Playground debugPrintln outputs only to the Console and doesn't appear in the playground itself.

    import Foundation
    
    class Tdesc: NSObject, Printable, DebugPrintable {
        override var description: String {return "A description"}
        override var debugDescription: String {return "A debugDescription"}
    }
    
    class Xdesc: Printable, DebugPrintable {
        var description: String {return "A description"}
        var debugDescription: String {return "A debugDescription"}
    }
    
    let t = Tdesc()
    let x = Xdesc()
    
    t.description
    
    let z: String = "x\(t)"
    
    println(t)      // Displays "A description" in the Playground and Console
    
    debugPrintln(t) // Displays nothing in the Playground but "A description" in the Console
    
    x.description
    
    let y: String = "x\(x)"
    
    println(x)      // Displays "__lldb_expr_405.Xdesc" in the Playground and Console
    
    debugPrintln(x)
    

提交回复
热议问题