LoginSignup
11
10

More than 5 years have passed since last update.

swiftでenumを文字列に変換する

Last updated at Posted at 2015-03-17
enum Environment {
    case Production
    case Staging
    case Development
    case Test

    var toString : String! {
        switch self {
        case Environment.Production:
            return "Production"
        case Environment.Staging:
            return "Staging"
        case Environment.Development:
            return "Development"
        case Environment.Test:
            return "Test"
        }
    }
}

上記のようなtoStringを作り

let env = Environment.Development
println("Now Environment is " + env.toString)

のようにできる。
最初から文字列のENUMでよいのなら、String型で指定し、

enum Environment : String {
    case Production = "Production"
    case Staging = "Staging"
    case Development = "Development"
    case Test = "Test"
}

とできる。

11
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
11
10