Difference between Printable and DebugPrintable in Swift

后端 未结 3 505
失恋的感觉
失恋的感觉 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:10

    I believe the main difference is the property that's used to print. Printable has description and DebugPrintable has debugDescription:

    https://stackoverflow.com/a/24254220/887210

    https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Printable.html#//apple_ref/doc/uid/TP40014608-CH11-SW1

    Edit:

    Apparently print() and println() don't work properly with Printable and DebugPrintable:

    struct TestPrintable : Printable {
      var description: String { return "Testing Printable" }
    }
    
    struct TestDebugPrintable : DebugPrintable {
      var debugDescription: String { return "Testing DebugPrintable" }
    }
    
    println(TestPrintable())      // -> "__lldb_expr_42.TestPrintable"
    println(TestDebugPrintable()) // -> "__lldb_expr_42.TestDebugPrintable"
    

    More information about this:

    http://vperi.com/2014/06/04/textual-representation-for-classes-in-swift/

提交回复
热议问题