加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用于字符串插入的Swift协议

发布时间:2020-12-14 05:35:04 所属栏目:百科 来源:网络整理
导读:我需要实现什么协议来控制 Swift中字符串插值中对象的表示方式? 我不会指定什么是打印在这样的东西: struct A{}var a = A()println("(a)") 您需要实现可打印协议: This protocol should be adopted by types that wish to customize their textual repre
我需要实现什么协议来控制 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读