以下面的枚举为例
enum Name : String {
case Me = "Prakash"
case You = "Raman"
}
我可以执行以下操作吗?
更改一个" case"的原始值别的什么。
Name.Me = "Prak"
向ENUM添加新案例
Name.Last = "Benjamin"
谢谢!
答案 0 :(得分:6)
Short answer: No, you can't.
Enumeration types are evaluated at compile time.
It's not possible to change raw values nor to add cases at runtime.
The only dynamic behavior is using associated values.
答案 1 :(得分:3)
不,你不能。相反,您可以重新定义枚举以包含关联值而不是原始值。
enum Name {
case Me(String)
case You(String)
case Last(String)
}
var me = Name.Me("Prakash")
print(me)
me = .You("Raman")
print(me)
me = .Last("Singh")
print(me)