在Swift中更新/更改枚举的rawValue [英] Update / change the rawValue of a enum in Swift

查看:384
本文介绍了在Swift中更新/更改枚举的rawValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的枚举为例

enum Name : String {

  case Me = "Prakash"
  case You = "Raman"

}

我可以执行以下操作

将一个案例的原始值更改为其他值。

Change the raw value of one "case" to something else.

Name.Me = "Prak"

在ENUM中添加新案例

Add a new case to the ENUM

Name.Last = "Benjamin"

谢谢!

推荐答案

简短的回答:不,你不能。

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.

参考: Swift语言指南:枚举

这篇关于在Swift中更新/更改枚举的rawValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆