20
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift] 列挙型(enum)についてメモ

Posted at

Swiftになって列挙型がだいぶ拡張されたようです。
こちらの記事を参考にしました。

通常のenum

enum Sample {
	case A
	case B, C, D
	case E, F
}

var samp = Sample.A

型を指定したenum

enum Sample: Int {
    case type1 = 0
    case type2, type3, type4
}

メソッドを持ったenum

enum Sample: Int {
    case type1 = 0
    case type2, type3, type4
    func str() -> String {
        switch self {
            case .type1:
                return "type1"
            case .type2:
                return "type2"
            case .type3:
                return "type3"
            case .type4:
                return "type4"
        }
    }
}

値を持ったenum

enum Sample {
    case Type1(String, String)
    case Type2(Int)
}

var samp = Sample.Type1("hoge", "fuga")

switch samp {
    case .Type1(let a, let b):
        println("\(a), \(b)")
        break
    
    case .Type2(let a):
        println(a)
        break
}
20
19
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
20
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?