TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Checking switch cases twice does not create an issue

Forums > 100 Days of SwiftUI

https://www.hackingwithswift.com/quick-start/beginners/how-to-use-switch-statements-to-check-multiple-conditions#:~:text=If%20you%20try%20changing%20.snow%20for%20.rain%2C%20you%E2%80%99ll%20see%20Swift%20complains%20loudly%3A%20once%20that%20we%E2%80%99ve%20checked%20.rain%20twice

In tutorial it is described as:

If you try changing .snow for .rain, you’ll see Swift complains loudly: once that we’ve checked .rain twice

I check the rainy case twice in switch case and it does not produce any error

enum Weather {
    case rainy
    case snowy
    case sunny
}

let test = Weather.sunny

switch test {
    case .rainy:
        print("hello")
    case .snowy:
        print("hello")
    case .sunny:
        print("hello")
    case .rainy:
        print("hello")
}

Why is that?

   

I check the rainy case twice in switch case and it does not produce any error.

I pasted your code into Playgrounds and you're right, it doesn't produce an error. But, for me, it did produce a warning on the second .rainy case.

⚠️ Case is already handled by previous patterns; consider removing it.

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.