WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.