如何在Swift 4中通过原始值获取枚举用例的名称?

时间:2018-08-29 11:52:12

标签: ios swift enums

使用Xcode 9.4.1和Swift 4.1

具有一个Int类型的多个案例的枚举,如何按其rawValue打印案例名称?

class Foo {
    var number: Int = 0; private set
    fun increment() {
       ++number;
    }
}

我正在通过rawValue访问枚举:

public enum TestEnum : UInt16{
case ONE    = 0x6E71
case TWO    = 0x0002
case THREE  = 0x0000
}

现在,我还想在十六进制值之后打印“一”。

我知道问题:How to get the name of enumeration value in Swift? 但这有所不同,因为我想通过案例原始值而不是枚举值本身来确定案例名称。

所需的输出:

命令类型= 0x6E71,一个

2 个答案:

答案 0 :(得分:4)

由于枚举类型不是<div class="widget-stencilbar-livescore" data-module="stencilbar/livescore" > <div class="widget-stencilbar-livescore__header"> <h1 class="widget-stencilbar-livescore__title"> {{ $headline }} </h1> @include('widgets.switch.index', [ 'default' => trans('widgets/stencilbar/livescore/index.competitions'), 'alt' => trans('widgets/stencilbar/livescore/index.kickoff') ]) </div> <div class="widget-stencilbar-livescore__icons"> <div class="widget-stencilbar-livescore__icon widget-stencilbar-livescore__icon--live-now" > <label class="widget-stencilbar-livescore__icon-label"> <input type="checkbox" class="widget-stencilbar-livescore__icon-checkbox" autocomplete="off" data-ga-label="Canli" > <span class="widget-stencilbar-livescore__icon-state"> </span> </label> </div> <div class="widget-stencilbar-livescore__icon widget-stencilbar-livescore__icon--iddaa widget-stencilbar-livescore__icon--should-be-at-fourth-place" > <label class="widget-stencilbar-livescore__icon-label"> <input type="checkbox" class="widget-stencilbar-livescore__icon-checkbox" autocomplete="off" data-ga-label="Iddaa" > <span class="widget-stencilbar-livescore__icon-state"> </span> </label> </div> <div class="widget-stencilbar-livescore__icon widget-stencilbar-livescore__icon--favourite widget-stencilbar-livescore__icon--not-clickable" > <label class="widget-stencilbar-livescore__icon-label"> <input type="checkbox" class="widget-stencilbar-livescore__icon-checkbox" autocomplete="off" data-ga-label="Favorites" > <span class="widget-stencilbar-livescore__icon-state"> </span> </label> </div> <div class="widget-stencilbar-livescore__icon widget-stencilbar-livescore__icon--duello widget-stencilbar-livescore__icon--not-clickable" > <label class="widget-stencilbar-livescore__icon-label"> <input type="checkbox" class="widget-stencilbar-livescore__icon-checkbox" autocomplete="off" data-ga-label="Duello" > <span class="widget-stencilbar-livescore__icon-state"> </span> </label> </div> </div> </div>,因此无法获得String那样的案例名称,因此您需要添加一种方法以自己返回它……

String

答案 1 :(得分:1)

您可以从其rawValue创建一个枚举值,并使用within try-block Exception in thread "main" java.io.IOException: Stream Closed at java.base/java.io.FileOutputStream.writeBytes(Native Method) at java.base/java.io.FileOutputStream.write(FileOutputStream.java:342) at Main.demoMethod(Main.java:24) at Main.main(Main.java:12) 获取其大小写字符串。

"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
}