gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:36:05 24 4
gpt4 key购买 nike

我想为给定的类型实现类似于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com