gpt4 book ai didi

swift - 如何在 swift 中为枚举添加更多案例

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:19 24 4
gpt4 key购买 nike

我正在尝试在 Swift 中实现以下目标。向枚举添加更多案例而不是编辑现有案例。

例如,我有以下枚举,我想使用扩展添加更多案例,而不是在原始枚举上进行编辑。

enum UIType: String, Codable {
case borderButton = "border_button"
case bottomSheet = "bottom_sheet"
}

现在如果我想向枚举中添加更多元素

case borderLabel = "border_Label"

最佳答案

这是不可能的。将 case 添加到扩展中的 enum 与为 classstruct 添加存储属性相当. (这也是不行的)

另请参阅文档:

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).— https://docs.swift.org/swift-book/LanguageGuide/Extensions.html

扩展只能添加新功能。

添加属性会改变存储对象所需的内存大小

关于swift - 如何在 swift 中为枚举添加更多案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53113895/

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