0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Swift]enumで変数が定義できる?

Posted at

目次

1.enumで変数が使える?
2.実例
3.おわりに

1. enumで変数が使える?

Swiftにおいて何かと便利に使用できるenumですが、実は変数(プロパティ)が定義できるってご存知ですか?
ちなみにenumは個人的にはデータ管理に非常に役立っていると感じます。

2. 実例

enumプロパティ
enum Month: Int {
    case january = 1
    case february = 2
    case march = 3
}

extension Month {
    var japanese: String {
        switch self {
        case .january:
            return "睦月"
        case .february:
            return "如月"
        case .march:
            return "卯月"
        }
    }
}

print("\(Month.january.rawValue)月")
print("日本語名:\(Month.january.japanese)")
// 出力結果
// 1月
// 日本語名:睦月

3. おわりに

いかがでしたか?enumを理解すると、変に文字列を使用しなくてよかったり、共同作業者のエンジニアとのすり合わせがうまくいったりと何かと便利です。
enumをしっかりと理解して効率の良い開発ができるようになりましょう♪

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?