SlideShare a Scribd company logo
1 of 15
Download to read offline
Swift 3 :
Enumeration
군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공
남 광 우
kwnam@kunsan.ac.kr
Swift 3 Tour and Language Guide by Apple
Enumeration
• 정의
• 관련된 값들을 하나의 그룹으로 묶기 위한 공통 타입
• 사용 목적
• 원하지 않는 값의 잘못 입력 방지
• 입력 값이 미리 특정 가능한 범위 내에 있을 때
• 제한된 값에서만 선택할 수 있게 강제할 때
enum SomeEnumeration {
// enumeration definition goes here
}
• Enum 사용 예
• 여러 개를 , 로 이어서 사용도 가능
enum CompassPoint {
case north
case south
case east
case west
}
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
Enumeration
Enumeration
• Enum 사용
• 변수에 할당
• 일단 할당되거나 타입 선언이 되어있다면
var directionToHead = CompassPoint.west
CompassPoint.west
CompassPoint.north
…
var directionToHead : CompassPoint = CompassPoint.west
directionToHead = .west
Enumeration
• Switch 구문 사용
directionToHead = .south
switch 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 = -1
case NULL = 0
case 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 = 11
if 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 value
case 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의 사용 예
• 전체 값의 상수 정의 가능 여부에 따라 실행

More Related Content

Viewers also liked

Swift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureSwift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureKwang Woo NAM
 
[공간정보시스템 개론] L04 항공사진의 이해
[공간정보시스템 개론] L04 항공사진의 이해[공간정보시스템 개론] L04 항공사진의 이해
[공간정보시스템 개론] L04 항공사진의 이해Kwang Woo NAM
 
Swift 3 Programming for iOS: Function
Swift 3 Programming for iOS: FunctionSwift 3 Programming for iOS: Function
Swift 3 Programming for iOS: FunctionKwang Woo NAM
 
Ieeepro techno solutions ieee dotnet project - privacy-preserving multi-keyw...
Ieeepro techno solutions  ieee dotnet project - privacy-preserving multi-keyw...Ieeepro techno solutions  ieee dotnet project - privacy-preserving multi-keyw...
Ieeepro techno solutions ieee dotnet project - privacy-preserving multi-keyw...ASAITHAMBIRAJAA
 
iOS 10 & XCode 8, Swift 3.0 features and changes
iOS 10 & XCode 8, Swift 3.0 features and changesiOS 10 & XCode 8, Swift 3.0 features and changes
iOS 10 & XCode 8, Swift 3.0 features and changesManjula Jonnalagadda
 
Swift 3 Programming for iOS : Closure
Swift 3 Programming for iOS  : ClosureSwift 3 Programming for iOS  : Closure
Swift 3 Programming for iOS : ClosureKwang Woo NAM
 
[공간정보시스템 개론] L07 원격탐사의 개념과 활용
[공간정보시스템 개론] L07 원격탐사의 개념과 활용[공간정보시스템 개론] L07 원격탐사의 개념과 활용
[공간정보시스템 개론] L07 원격탐사의 개념과 활용Kwang Woo NAM
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector MachinesDaegeun Lee
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageHossam Ghareeb
 
[공간정보시스템 개론] L11 공간정보의 구축
[공간정보시스템 개론] L11 공간정보의 구축[공간정보시스템 개론] L11 공간정보의 구축
[공간정보시스템 개론] L11 공간정보의 구축Kwang Woo NAM
 
[공간정보시스템 개론] L12 공간정보분석
[공간정보시스템 개론] L12 공간정보분석[공간정보시스템 개론] L12 공간정보분석
[공간정보시스템 개론] L12 공간정보분석Kwang Woo NAM
 
[공간정보시스템 개론] L01 공간정보시스템개요
[공간정보시스템 개론] L01 공간정보시스템개요[공간정보시스템 개론] L01 공간정보시스템개요
[공간정보시스템 개론] L01 공간정보시스템개요Kwang Woo NAM
 
[공간정보시스템 개론] L09 공간 데이터 모델
[공간정보시스템 개론] L09 공간 데이터 모델[공간정보시스템 개론] L09 공간 데이터 모델
[공간정보시스템 개론] L09 공간 데이터 모델Kwang Woo NAM
 
[공간정보시스템 개론] L03 지구의형상과좌표체계
[공간정보시스템 개론] L03 지구의형상과좌표체계[공간정보시스템 개론] L03 지구의형상과좌표체계
[공간정보시스템 개론] L03 지구의형상과좌표체계Kwang Woo NAM
 
[공간정보시스템 개론] L02 공간정보와 지리정보
[공간정보시스템 개론] L02 공간정보와 지리정보[공간정보시스템 개론] L02 공간정보와 지리정보
[공간정보시스템 개론] L02 공간정보와 지리정보Kwang Woo NAM
 
Support Vector Machine Tutorial 한국어
Support Vector Machine Tutorial 한국어Support Vector Machine Tutorial 한국어
Support Vector Machine Tutorial 한국어Jungkyu Lee
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageHossam Ghareeb
 

Viewers also liked (20)

Swift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureSwift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structure
 
[공간정보시스템 개론] L04 항공사진의 이해
[공간정보시스템 개론] L04 항공사진의 이해[공간정보시스템 개론] L04 항공사진의 이해
[공간정보시스템 개론] L04 항공사진의 이해
 
Swift 3 Programming for iOS: Function
Swift 3 Programming for iOS: FunctionSwift 3 Programming for iOS: Function
Swift 3 Programming for iOS: Function
 
Ieeepro techno solutions ieee dotnet project - privacy-preserving multi-keyw...
Ieeepro techno solutions  ieee dotnet project - privacy-preserving multi-keyw...Ieeepro techno solutions  ieee dotnet project - privacy-preserving multi-keyw...
Ieeepro techno solutions ieee dotnet project - privacy-preserving multi-keyw...
 
iOS 10 & XCode 8, Swift 3.0 features and changes
iOS 10 & XCode 8, Swift 3.0 features and changesiOS 10 & XCode 8, Swift 3.0 features and changes
iOS 10 & XCode 8, Swift 3.0 features and changes
 
Swift 3 Programming for iOS : Closure
Swift 3 Programming for iOS  : ClosureSwift 3 Programming for iOS  : Closure
Swift 3 Programming for iOS : Closure
 
Hacker News
Hacker NewsHacker News
Hacker News
 
[공간정보시스템 개론] L07 원격탐사의 개념과 활용
[공간정보시스템 개론] L07 원격탐사의 개념과 활용[공간정보시스템 개론] L07 원격탐사의 개념과 활용
[공간정보시스템 개론] L07 원격탐사의 개념과 활용
 
What's new in Swift 3
What's new in Swift 3What's new in Swift 3
What's new in Swift 3
 
Swift 3
Swift   3Swift   3
Swift 3
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
[공간정보시스템 개론] L11 공간정보의 구축
[공간정보시스템 개론] L11 공간정보의 구축[공간정보시스템 개론] L11 공간정보의 구축
[공간정보시스템 개론] L11 공간정보의 구축
 
[공간정보시스템 개론] L12 공간정보분석
[공간정보시스템 개론] L12 공간정보분석[공간정보시스템 개론] L12 공간정보분석
[공간정보시스템 개론] L12 공간정보분석
 
[공간정보시스템 개론] L01 공간정보시스템개요
[공간정보시스템 개론] L01 공간정보시스템개요[공간정보시스템 개론] L01 공간정보시스템개요
[공간정보시스템 개론] L01 공간정보시스템개요
 
[공간정보시스템 개론] L09 공간 데이터 모델
[공간정보시스템 개론] L09 공간 데이터 모델[공간정보시스템 개론] L09 공간 데이터 모델
[공간정보시스템 개론] L09 공간 데이터 모델
 
[공간정보시스템 개론] L03 지구의형상과좌표체계
[공간정보시스템 개론] L03 지구의형상과좌표체계[공간정보시스템 개론] L03 지구의형상과좌표체계
[공간정보시스템 개론] L03 지구의형상과좌표체계
 
[공간정보시스템 개론] L02 공간정보와 지리정보
[공간정보시스템 개론] L02 공간정보와 지리정보[공간정보시스템 개론] L02 공간정보와 지리정보
[공간정보시스템 개론] L02 공간정보와 지리정보
 
Support Vector Machine Tutorial 한국어
Support Vector Machine Tutorial 한국어Support Vector Machine Tutorial 한국어
Support Vector Machine Tutorial 한국어
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
 

More from Kwang Woo NAM

메타버스시대의_디지털트윈과_지역성v01.pdf
메타버스시대의_디지털트윈과_지역성v01.pdf메타버스시대의_디지털트윈과_지역성v01.pdf
메타버스시대의_디지털트윈과_지역성v01.pdfKwang Woo NAM
 
해양디지털트윈v02.pdf
해양디지털트윈v02.pdf해양디지털트윈v02.pdf
해양디지털트윈v02.pdfKwang Woo NAM
 
Moving objects media data computing(2019)
Moving objects media data computing(2019)Moving objects media data computing(2019)
Moving objects media data computing(2019)Kwang Woo NAM
 
Moving Objects and Spatial Data Computing
Moving Objects and Spatial Data ComputingMoving Objects and Spatial Data Computing
Moving Objects and Spatial Data ComputingKwang Woo NAM
 
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석Kwang Woo NAM
 
Swift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionSwift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionKwang Woo NAM
 
Swift 3 Programming for iOS : Control flow
Swift 3 Programming for iOS : Control flowSwift 3 Programming for iOS : Control flow
Swift 3 Programming for iOS : Control flowKwang Woo NAM
 
Swift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeSwift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeKwang Woo NAM
 
Swift 3 Programming for iOS
Swift 3 Programming for iOSSwift 3 Programming for iOS
Swift 3 Programming for iOSKwang Woo NAM
 
집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링Kwang Woo NAM
 
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01Kwang Woo NAM
 

More from Kwang Woo NAM (11)

메타버스시대의_디지털트윈과_지역성v01.pdf
메타버스시대의_디지털트윈과_지역성v01.pdf메타버스시대의_디지털트윈과_지역성v01.pdf
메타버스시대의_디지털트윈과_지역성v01.pdf
 
해양디지털트윈v02.pdf
해양디지털트윈v02.pdf해양디지털트윈v02.pdf
해양디지털트윈v02.pdf
 
Moving objects media data computing(2019)
Moving objects media data computing(2019)Moving objects media data computing(2019)
Moving objects media data computing(2019)
 
Moving Objects and Spatial Data Computing
Moving Objects and Spatial Data ComputingMoving Objects and Spatial Data Computing
Moving Objects and Spatial Data Computing
 
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석
세월호/ 타이타닉호 사고의 빅 데이터 방법론적 분석
 
Swift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionSwift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : Collection
 
Swift 3 Programming for iOS : Control flow
Swift 3 Programming for iOS : Control flowSwift 3 Programming for iOS : Control flow
Swift 3 Programming for iOS : Control flow
 
Swift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeSwift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data type
 
Swift 3 Programming for iOS
Swift 3 Programming for iOSSwift 3 Programming for iOS
Swift 3 Programming for iOS
 
집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링
 
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01
집단지성 프로그래밍 07-고급 분류 기법-커널 기법과 svm-01
 

Swift 3 Programming for iOS : Enumeration

  • 1. Swift 3 : Enumeration 군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공 남 광 우 kwnam@kunsan.ac.kr Swift 3 Tour and Language Guide by Apple
  • 2. Enumeration • 정의 • 관련된 값들을 하나의 그룹으로 묶기 위한 공통 타입 • 사용 목적 • 원하지 않는 값의 잘못 입력 방지 • 입력 값이 미리 특정 가능한 범위 내에 있을 때 • 제한된 값에서만 선택할 수 있게 강제할 때 enum SomeEnumeration { // enumeration definition goes here }
  • 3. • Enum 사용 예 • 여러 개를 , 로 이어서 사용도 가능 enum CompassPoint { case north case south case east case west } enum Planet { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune } Enumeration
  • 4. Enumeration • Enum 사용 • 변수에 할당 • 일단 할당되거나 타입 선언이 되어있다면 var directionToHead = CompassPoint.west CompassPoint.west CompassPoint.north … var directionToHead : CompassPoint = CompassPoint.west directionToHead = .west
  • 5. Enumeration • Switch 구문 사용 directionToHead = .south switch 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") }
  • 6. 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 사용
  • 7. • Enum에 값 할당 • 할당된 값의 사용 Raw Value Enum ErrorCodeEnum : Int { case FILE_NOT_EXIST = -1 case NULL = 0 case OK = 1 } Print( ErrorCodeEnum.OK.rawValue );
  • 8. 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 }
  • 9. 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"
  • 10. Raw Value • 값의 초기화 할당 • 특정 rawValue 값을 할당 • 값의 사용 let possiblePlanet = Planet(rawValue: 7) // possiblePlanet is of type Planet? and equals Planet.uranus let positionToFind = 11 if 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"
  • 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) }
  • 12. func evaluate(_ expression: ArithmeticExpression) -> Int { switch expression { case let .number(value): return value case 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 결합 사용
  • 13. • 사용 시점에 보조 값을 할당 • 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")
  • 14. 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")
  • 15. 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의 사용 예 • 전체 값의 상수 정의 가능 여부에 따라 실행