swift - 在 Swift 中是否有 CustomStringConvertible 等效类型?

标签 swift swift-protocols

我想为给定的类型实现类似于 CustomStringConvertible 的协议(protocol),但例如。

我的需要是显示类型属性 值,而无需为此目的创建实例评估器。当然,我可以向此类型添加 CustomStringConvertible 协议(protocol),然后创建一个实例值,仅用于调用 description。但是不知道Swift有没有这个功能呢?

最佳答案

概述:

  • type(of:) 是一个返回 String
  • 中实例类型的函数
  • self 可以用在 Type 上

代码:

struct Something {
    
}

print(type(of: Something.self))

关于swift - 在 Swift 中是否有 CustomStringConvertible 等效类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253807/

相关文章:

Swift 3 重构 : "Type ' NSView' has no member . .."错误

ios - 具有原始值的枚举,可编码

string - 角色的最后一个实例

ios - Swift:单击后退按钮后错过标签栏

ios - 相机没有指向我想要使用 SceneKit 的地方

ios - 在 textField 集合中查找空的 textField

ios - Swift 3 转换错误/NSError

swift - 动态调度协议(protocol)扩展不适用于多个目标

swift - Comparable 协议(protocol)可以通用吗?

ios - 如何使用 jazzy 来记录我的 swift 项目?