用于字符串插入的Swift协议

前端之家收集整理的这篇文章主要介绍了用于字符串插入的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

原文链接:https://www.f2er.com/swift/319664.html

猜你在找的Swift相关文章