페이지

2017년 2월 17일 금요일

Swift Enumeration,Associated values

//1. Enumerations



enum SomeEnumeration {

}

enum CompassPoint {
    case north
    case south
    case east
    case west
}

enum Planet {
    case Sun,Mercury,Earth,Mars,Jupiter,Neptune
}

var directionToHead = CompassPoint.north
print(directionToHead)
directionToHead = .west
print(directionToHead)

switch directionToHead {
    case .north:
        print("switch: \(directionToHead)")
    case .south:
        print("switch: \(directionToHead)")
    case .east:
        print("switch: \(directionToHead)")
    case .west:
        print("switch: \(directionToHead)")
}


//2. Associated Values

enum BarCode {
    case upc(Int,Int,Int,Int)
    case qrCode(String)
}

var bCode = BarCode.upc(1,3,4,66)
print(bCode)
bCode = .qrCode("hi hello")
print(bCode)

switch bCode {
    case .upc(let leftPos,let topPos,let rightPos,let bottomPos):
        print ("upc \(leftPos), \(topPos), \(rightPos), \(bottomPos)")
    case let .qrCode(name):
        print ("hey \(name)")
}

댓글 없음:

댓글 쓰기