LoginSignup
12
10

More than 5 years have passed since last update.

kotlinでenum classを使う

Last updated at Posted at 2016-08-23

数字から文字列を得たいとき、
(例えば5を指定して, Oneを文字列として得たいとき)

kotlin

enum class Financing(val rawValue :Int)  {
    One(5),
    Revolving(6),
    Two(7)
}
// 使う
Financing.values().filter { 5 == it.rawValue }.first() // => One (Financing型)
Financing.values().filter { 5 == it.rawValue }.first().toString() // => One(String型)

swiftコードをkotlinに変換しているとき、

kotlin

class Compass {
    enum class Direction(val rawValue: Int)  {
        NORTH(1),
        SOUTH(2),
        WEST(3),
        EAST(4)
    }
}

// 使う
Compass.Direction.NORTH.rawValue // => 1
swift
class Compass {
    enum Direction: Int  {
        NORTH = 1
        SOUTH
        WEST
        EAST
    }
}

// 使う
Compass.Direction.NORTH.rawValue // => 1
12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10