enum 케이스 나누기
학교를 선언
enum School {
case elementary
case middle
case high
}
아래처럼 선언해도 된다.
enum School {
case elementary, middle, high
}
let yourSchool = School.elementary
print("yourSchool : \(yourSchool)")
// yourSchool : elementary
학년 선언
enum Grade : Int {
case first = 1
case second = 2
}
값
let yourGrade = Grade.second
// second
값을가지고 있는 값 rawValue
let yourGrade = Grade.second.rawValue
// 2
학교 상세
enum SchoolDetail {
case elemantary(name: String)
case middle(name: String)
case high(name: String)
func getName() -> String {
switch self {
case let .elemantary(name):
return name
case let .middle(name):
return name
case let .high(name):
return name
}
}
}
아래처럼 작성 가능
enum SchoolDetail {
case elemantary(name: String)
case middle(name: String)
case high(name: String)
func getName() -> String {
switch self {
case .elemantary(let name):
return name
case .middle(let name):
return name
case .high(let name):
return name
}
}
}
let yourMiddleSchoolName = SchoolDetail.middle(name: "mySchool")
print(yourMiddleSchoolName)
// middle(name: "mySchool")
값만 가지고 올 경우
print(yourMiddleSchoolName.getName())
// mySchool
반응형