swift 3 : enumeration

15
Swift 3 : Enumeration 군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공 남광우 [email protected] Swift 3 Tour and Language Guide by Apple

Upload: kwang-woo-nam

Post on 13-Jan-2017

42 views

Category:

Software


0 download

TRANSCRIPT

Swift 3 : Enumeration

군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공

남 광 우

[email protected]

Swift 3 Tour and Language Guide by Apple

Enumeration• 정의• 관련된 값들을 하나의 그룹으로 묶기 위한 공통 타입

• 사용 목적• 원하지 않는 값의 잘못 입력 방지• 입력 값이 미리 특정 가능한 범위 내에 있을 때• 제한된 값에서만 선택할 수 있게 강제할 때

enum SomeEnumeration {// enumeration definition goes here

}

• Enum 사용 예

• 여러 개를 , 로 이어서 사용도 가능

enum CompassPoint {case northcase southcase eastcase west

}

enum Planet {case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune

}

Enumeration

Enumeration• Enum 사용

• 변수에 할당

• 일단 할당되거나 타입 선언이 되어있다면

var directionToHead = CompassPoint.west

CompassPoint.westCompassPoint.north…

var directionToHead : CompassPoint = CompassPoint.west

directionToHead = .west

Enumeration• Switch 구문 사용

directionToHead = .southswitch directionToHead {case .north:

print("Lots of planets have a north")case .south:

print("Watch out for penguins")case .east:

print("Where the sun rises")case .west:

print("Where the skies are blue")}

let somePlanet = Planet.earth

switch somePlanet {case .earth:

print("Mostly harmless")default:

print("Not a safe place for humans")}// Prints "Mostly harmless"

Enumeration• Switch 구문의 default 사용

• Enum에 값 할당

• 할당된 값의 사용

Raw Value

Enum ErrorCodeEnum : Int {case FILE_NOT_EXIST = -1case NULL = 0case OK = 1

}

Print( ErrorCodeEnum.OK.rawValue );

Raw Value • Enum 값의 묵시적 할당• 첫번째 할당된 값을 기준으로 +1씩 할당

• 만약 -2 부터 시작한다면?• Earth == 0

enum Planet: Int {case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune

}

enum Planet: Int {case mercury = -2, venus, earth, mars, jupiter, saturn, uranus, neptune

}

Raw Value • Enum 값의 묵시적 할당(String)

• String일 경우 enum명이 기본 값• CompassPoint.north.rawValue == “north”

• 사용 예

enum CompassPoint: String {case north, south, east, west

}

let earthsOrder = Planet.earth.rawValue// earthsOrder is 3

let sunsetDirection = CompassPoint.west.rawValue// sunsetDirection is "west"

Raw Value • 값의 초기화 할당• 특정 rawValue 값을 할당

• 값의 사용

let possiblePlanet = Planet(rawValue: 7)// possiblePlanet is of type Planet? and equals Planet.uranus

let positionToFind = 11if let somePlanet = Planet(rawValue: positionToFind) {

switch somePlanet {case .earth:

print("Mostly harmless")default:

print("Not a safe place for humans")}

} else {print("There isn't a planet at position \(positionToFind)")

}// Prints "There isn't a planet at position 11"

enum ArithmeticExpression {case number(Int)indirect case addition( ArithmeticExpression, ArithmeticExpression)indirect case multiplication( ArithmeticExpression, ArithmeticExpression)

}

Recursive• Enum의 recursive 정의• Indirect 키워드 사용

• Enum안의 모든 property가 recursive 하다면indirect enum ArithmeticExpression {

case number(Int)case addition(ArithmeticExpression, ArithmeticExpression)case multiplication(ArithmeticExpression, ArithmeticExpression)

}

func evaluate(_ expression: ArithmeticExpression) -> Int {switch expression {case let .number(value):

return valuecase let .addition(left, right):

return evaluate(left) + evaluate(right)case let .multiplication(left, right):

return evaluate(left) * evaluate(right)}

}print(evaluate(product))// Prints "18"

let five = ArithmeticExpression.number(5)let four = ArithmeticExpression.number(4)let sum = ArithmeticExpression.addition(five, four)let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))

Recursive• Enum의 recursive 사용

• Switch 결합 사용

• 사용 시점에 보조 값을 할당• C에서의 Union처럼 사용

• 예 : 바코드

Associated Value : 연관 값

UPC(int, int, int, int) QRCode(String) : 2,053 chars

enum Barcode {case upc(Int, Int, Int, Int)case qrCode(String)

}

var productBarcode = Barcode.upc(8, 85909, 51226, 3)productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

Associated Value : 연관 값

• Associated Value의 사용• Case를 이용해서 현재 할당된 값에 따라 수행

switch productBarcode {

case .upc(let numberSystem, let manufacturer, let product, let check):print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")

case .qrCode(let productCode):print("QR code: \(productCode).")

}// Prints "QR code: ABCDEFGHIJKLMNOP.

productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

switch productBarcode {case let .upc(numberSystem, manufacturer, product, check):

print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")

case let .qrCode(productCode):print("QR code: \(productCode).")

}// Prints "QR code: ABCDEFGHIJKLMNOP."

Associated Value : 연관 값

• Associated Value의 사용 예• 전체 값의 상수 정의 가능 여부에 따라 실행