Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active April 7, 2017 04:38
Show Gist options
  • Save KentarouKanno/1774b229f1555c1f20a2 to your computer and use it in GitHub Desktop.
Save KentarouKanno/1774b229f1555c1f20a2 to your computer and use it in GitHub Desktop.
enum

enum

enum Season {
    case Spring
    case Summer
    case Autumn
    case Winter
}

let season = Season.Summer

// ----------------------------------

enum Season: String {
    case Spring
    case Summer
    case Autumn
    case Winter
}

let num = Season.Summer.hashValue
//=> num = 1

let stringValue1 = Season.Summer.rawValue
//=> stringValue1 = "Summer"

let stringValue2 = Season.Summer.rawValue.description
//=> stringValue2 = "Summer"

let stringValue3 = Season.Summer.rawValue.capitalized
//=> stringValue3 = "Summer"

// ----------------------------------

// Int
enum Season: Int {
    case Spring  = 1
    case Summer // 2
    case Autumn // 3
    case Winter // 4
}

let season = Season.Autumn

let intValue1 = season.hashValue
//=> intValue1 = 2

let intValue2 = season.rawValue
//=> intValue2 = 3

let stringValue = season.rawValue.description
//=> stringValue = "3"

// ----------------------------------

// Int(数値を明示的に指定)
enum Season: Int {
    case Spring = 10
    case Summer = 20 
    case Autumn = 30
    case Winter = 40
}

// ----------------------------------

enum Season: String {
    case Spring = ""
    case Summer = ""
    case Autumn = ""
    case Winter = ""
}

let value1 = Season.Winter.rawValue
//=> value1 = "冬"

let value2 = Season.Winter.rawValue.capitalized
//=> value2 = "冬"

let value3 = Season.Winter.rawValue.description
//=> value3 = "冬"

do {
    
    enum Direction {
        case up
        case down
        case right
        case left
    }
}

// ----------------------------------

do {
    enum Direction {
        case up, down, right, left
    }
    
    let d = Direction.up
    var x: Direction = .right
    
    if d == x {
        
    }
}

// ----------------------------------

do {
    enum Direction {
        case up, down, right, left
        
        func clockwise() -> Direction {
            switch self {
            case .up: return .right
            case .right: return .down
            case .down: return .left
            case .left: return .up
            }
        }
    }
    
    let d = Direction.up
    d.clockwise() == Direction.down
    d.clockwise().clockwise() == Direction.down
}

// ----------------------------------

do {
    enum Direction: Int  {
        case up = 0, down, right, left
    }
    
    let a = Direction.right
    let i = a.rawValue
    let k = Direction.down.rawValue
}

// ----------------------------------

do {
    enum Direction: String  {
        case up, down, right, left
    }
    
    Direction.right.rawValue
    Direction.right.hashValue
    
    let k = Direction.down.rawValue
    //=> "down"
}

// ----------------------------------

do {
    
    enum Direction: Int {
        case up, down, right, left
        
    }
    
    let b: Direction? = Direction(rawValue: 3)
    b! == Direction(rawValue: 2)
    
    if let c = Direction(rawValue: 2) {
        print("OK ", c.rawValue)
    }
}

// ----------------------------------

do {
    
    enum Direction: Int {
        case up = 0, right, down, left
        
        func clockwise() -> Direction {
            let t = (self.rawValue + 1) % 4
            return Direction(rawValue: t)!
        }
    }
    
    var a = Direction.up
    a.clockwise()
}

// ----------------------------------

do {
    
    enum Direction: Int {
        case up = 0, right, down, left
        
        var horizontal: Bool {
            switch self {
            case .right, .left: return true
            default: return false
                
            }
        }
        
        mutating func turnBack() {
            self = Direction(rawValue: ((self.rawValue + 2) % 4))!
        }
    }
    
    var d = Direction.left
    d.rawValue
    d.turnBack()
    d.rawValue
    d.horizontal
    
    var a = Direction(rawValue: 0)
}

// ----------------------------------

do {
    
    enum Direction: Int {
        case up, right, down, left
        
        static var defaultDirection = Direction.up
        init() {
            self = Direction.defaultDirection
        }
        
        static func arrow(d: Direction) -> String {
            return ["", "", "", ""][d.rawValue]
        }
    }
    
    Direction.defaultDirection = .right
    var e = Direction()
    Direction.arrow(d: e)
}

// ----------------------------------

do {
    
    enum WebColor {
        case name(String)
        case code(String)
        case white, black, red
    }
    
    let background = WebColor.name("indigo")
    let turquoise: WebColor = .code("#40E0D0")
    let textColor = WebColor.black
    
    // if turquoise == WebColor.Code("#40E0D0") {}
}

// ----------------------------------


do {
    enum Ticket {
        case 切符(Int, Bool, 回数券: Bool)
        case カード(Int, Bool)
        case 敬老バス
    }
    
    // var t = Ticket.切符(300, true, 回数券: false)
    // var t = Ticket.敬老バス
    var t = Ticket.カード(200, false)
    
    switch t {
    case let .切符(fare, flag, _): print("普通券 : ", fare , flag ? "子供" : "大人")
    case .敬老バス: print("敬老バス")
    case .カード(let r, true) where r < 110 : print("カード : 残額不足")
    case .カード(let r , false) where r < 230 : print("カード : 残額不足")
    case .カード: print("カード")
    }
}

// ----------------------------------


do {
    
    enum Direction: String {
        case up = "", right = "", down = "", left = ""
    }
    
    let a = Direction.up.rawValue
    //=> "上"
    let b = Direction.up.hashValue
    //=> 0
    
    if let c = Direction(rawValue: "") {
        
        // let d = Direction(rawValue: 0)
        print(c)
    }
}

// ----------------------------------

class MyLiteral: ExpressibleByIntegerLiteral, Equatable {
    var value: Int
    
    required init(integerLiteral value: IntegerLiteralType) {
        self.value = value
    }
}


func ==(lhs: MyLiteral, rhs: MyLiteral) -> Bool {
    return lhs.value == rhs.value
}

enum  MyEnum: MyLiteral {
    case once = 1
    case twice = 2
}

let myenum1 = MyEnum.once
let myenum2 = MyEnum.twice

if myenum1 == myenum2 {
    print("Equal")
} else {
    print("Mot Equal")
}


// ----------------------------------



do {
    enum MyEnum {
        case a, b, c
        
        func printSelf() -> () {
            print(self)
        }
        
        mutating func mutatingSelf() {
            printSelf()
            self = .a
            printSelf()
            self = .b
            printSelf()
            self = .c
            printSelf()
        }
    }
    
    var a = MyEnum.a
    a.mutatingSelf()
}


do {
    enum Aenum { case a, b, c
        
        func myStatus() {
            switch self {
            case .a: ()
                
            default: ()
            }
        }
    }
    
    let a = Aenum.b
    a.myStatus()
}

★ defaultではなく_を使用する

enum Sample1 {
    case a
    case b
    case c
}

let a = Sample1.a

switch a {
case .a:
    break
case _:
    break
}

// default の代わりに_を使用する

★ EnumでAssociated Value

class A {}
class B {}
class C {}
class D {}
class E {}
class F {}


enum EnumType1 {
    case type1(A, E, F)
    case type2(B)
    case type3(EnumType2)
}

enum EnumType2 {
    case type1(C)
    case type2(D)
}


let v1 = EnumType1.type1(A(), E(), F())
let v2 = EnumType1.type2(B())
let v3 = EnumType1.type3(EnumType2.type1(C()))

let v4 = EnumType2.type1(C())
let v5 = EnumType2.type2(D())

switch v1 {
    
case .type1(let a, let e, let f): print(a, e, f)
case .type2(let b): print(b)
case .type3(let enumType):
    
    switch enumType {
    case .type1(let c): print(c)
    case .type2(let d): print(d)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment