This protocol should be adopted by types that wish to customize their
textual representation. This textual representation is used when
objects are written to anOutputStreamType
.
protocol Printable { var description: String { get } }
当DebugPrintable协议仅用于调试时,还有DebugPrintable协议:
This protocol should be adopted by types that wish to customize
their textual representation used for debugging purposes. This
textual representation is used when objects are written to an
OutputStreamType
.
protocol DebugPrintable { var debugDescription: String { get } }
Documentation(感谢@MartinR)
注意:在评论中提到的@Antonio和@MartinR,这在操场上(从Xcode6 GM开始)不起作用;这是一个已知的错误.它在编译应用程序中工作.
从Xcode6 GM发行说明:
In Playgrounds,println() ignores the Printable conformance of
user-defined types. (16562388)
截至目前,Swift 2.0 Printable已成为CustomStringConvertible.一切都保持与以前一样,你仍然需要实现
var description: String { get }
但现在它叫CustomStringConvertible.而调试是CustomDebugStringConvertible