-
[Swift] enum 열거형iOS/Swift 2022. 12. 1. 01:58
Swift 공식 문서 (한국어 가이드)
https://jusung.gitbook.io/the-swift-language-guide/language-guide/08-enumerations
import UIKit // 학교 - 초, 중, 고 enum School_1 { case elementary case middle case high } enum School_2 { case elementary, middle, high } let yourSchool = School_1.high print("yourSchool: \(yourSchool)") print("yourSchool: ", yourSchool) enum Grade : Int { case first = 1 case second = 2 } let yourGrade = Grade.second print("yourGrade: \(yourGrade)") print("yourGrade: \(yourGrade.rawValue)") let yourGrade_2 = Grade.second.rawValue print("yourGrade_2: \(yourGrade_2)") enum SchoolDetail { case elementary(name: String) case middle(name: String) case high(name: String) func getName() -> String {//getName()을 호출하면 String을 반환하겠다. switch self { // 만약 SchoolDetail 나 자신이 ~ 라면 case .elementary(let name): return name case let .middle(name): return name case .high(let name): return name }//switch end }//func end }//enum let yourMiddleSchooldName = SchoolDetail.middle(name: "단비중학교") print("yourMiddleSchooldName: \(yourMiddleSchooldName)") //yourMiddleSchooldName: middle(name: "단비중학교") print("yourMiddleSchooldName: \(yourMiddleSchooldName.getName())") //yourMiddleSchooldName: 단비중학교
enum 예제
enum BookmarkActionType { case add case delete } let action: BookmarkActionType switch action { case .add: // 스토어를 찜했을 때 동작하는 작업 case .delete: // 찜한 스토어를 삭제했을 때 동작하는 작업 } [출처] https://labs.brandi.co.kr/2021/09/01/kimjh3.html
'iOS > Swift' 카테고리의 다른 글
[Swift] 메소드 (Methods) (0) 2022.12.02 [Swift] 클래스와 스트럭트 (0) 2022.12.01 [Swift] foreach 반복문 (0) 2022.12.01 [Swift] 조건문 (0) 2022.12.01 [Swift] Unwrap 옵셔널 변수 (0) 2022.12.01