[swift] guard 문을 사용하여 스위프트에서의 enum 일치 여부를 검사할 수 있나요?

guard 문은 조건이 false인 경우 실행 흐름을 빠져나가는데 사용됩니다. 이를 통해 특정한 조건이 충족되지 않는 상황을 다른 순서로 처리할 수 있습니다.

아래는 guard 문을 사용하여 enum의 일치 여부를 검사하는 간단한 예제입니다.

enum Weather {
    case sunny
    case rainy
    case cloudy
}

func getWeatherDescription(weather: Weather) {
    guard case .sunny = weather else {
        print("It's not sunny")
        return
    }
    print("It's sunny")
}

let todayWeather = Weather.sunny
getWeatherDescription(weather: todayWeather)

위의 코드에서는 guard 문을 사용하여 날씨를 확인하고, 날씨가 맑을 때 메시지를 출력하고 그렇지 않을 경우 다른 메시지를 출력하도록 설정되어 있습니다.

자세한 내용은 Swift 공식 문서를 참고하시기 바랍니다.