如何快速添加更多的枚举案例 [英] How to add more cases for enum in swift
问题描述
我正在尝试在Swift中实现以下目标。
向枚举添加更多案例,而不是在现有的案例上进行编辑。
I am trying to achieve the following in Swift. Adding more cases to enum rather than editing on the existing one.
例如,我有以下枚举,并且我想使用扩展名添加更多案例,不要在原始枚举上进行编辑。
For example, I have the following enum and I want to add more cases using extension, not to edit on the original enum.
enum UIType: String, Codable {
case borderButton = "border_button"
case bottomSheet = "bottom_sheet"
}
现在,如果我想向枚举添加更多元素
Now if I want to add more elements to enum
case borderLabel = "border_Label"
推荐答案
这是不可能的。
在扩展名的枚举
中添加情况
相当于添加(未计算)属性
用于类
或结构
。 (这也是不可能的)
This is not possible.
Adding a case
to an enum
in an extension is comparable to add (not computed) property
for a class
or struct
. (This is also not possible)
另请参见文档:
扩展名向现有的类,结构,枚举或协议类型添加新功能。这包括扩展您无法访问原始源代码的类型的能力(称为追溯建模)。
— https://docs.swift.org/swift-book/ LanguageGuide / Extensions.html
扩展可以添加新功能。
添加属性会更改内存大小需要使用巫婆来存储对象。
Adding properties would change the the memory size witch is needed to store an object.
这篇关于如何快速添加更多的枚举案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!