Swift で enumを定義する
以下のように enum で環境を定義して、そこにAPIのBase URLを持たせることをよくする。
enum Env: Int { case Local case Staging case Production func baseURL() -> String { switch(self) { case .Local: return "http://localhost:3000" case .Staging: return "http://stg.hoge.com" case .Production: return "http://hoge.com" } } func description() -> String { switch(self) { case .Local: return "Local" case .Staging: return "Staging" case .Production: return "Production" } } }
enum から Int 型への変換は以下の通り rawValue
というメソッドを使う。
Env.Local.rawValue