본문 바로가기
Mobile/Swift

switch

by 꼰대코더 2024. 2. 14.

enum

enum Weather {
    case sun, rain, wind, snow, unknown
}

let forecast = Weather.sun

switch forecast {
     case .sun:
         print("It should be a nice day.")
     case .rain:
         print("Pack an umbrella.")
     case .wind:
         print("Wear something warm")
     case .snow:
         print("School is cancelled.")
     case .unknown:
         print("Our forecast generator is broken!")
}

 

String

let place = "Metropolis"

switch place {
     case "Gotham":
         print("You're Batman!")
     case "Mega-City One":
         print("You're Judge Dredd!")
     case "Wakanda":
         print("You're Black Panther!")
     default:
         print("Who are you?")
}

 

Int

let day = 5

switch day {
    case 5:
        print("5 golden rings")
         fallthrough
    case 4:
        print("4 calling birds")
    case 3:
        print("3 French hens")
    case 2:
        print("2 turtle doves")
    default:
        print("A partridge in a pear tree")
}
5 golden rings
4 calling birds
fallthrough 에 의해 아래 case 4까지 실행

 

'Mobile > Swift' 카테고리의 다른 글

function  (0) 2024.02.14
Loops  (0) 2024.02.14
String  (0) 2024.02.08
enum  (0) 2024.02.07
Dictionary  (1) 2024.02.07