用于字符串插入的Swift协议

我需要实现什么协议来控制 Swift中字符串插值中对象的表示方式?

我不会指定什么是打印在这样的东西:

struct A{

}

var a = A()
println("\(a)")
您需要实现可打印协议:

This protocol should be adopted by types that wish to customize their
textual representation. This textual representation is used when
objects are written to an OutputStreamType.

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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...