UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Need help with switch statements

Forums > SwiftUI

let weather = "sunny" switch weather { case "rain": print("Bring an umbrella") case "snow": print("Wrap up warm") case "sunny": print("Wear sunscreen") fallthrough default: print("Enjoy your day!") } This code isn't working for some reason. This code aint workin'.

2      

It worked for me. This is what it printed:

  • Wear sunscreen
  • Enjoy your day!
import Foundation

let weather = "sunny"

switch weather {
    case "rain":
    print("Bring an umbrella")
    case "snow":
    print("Wrap up warm")
    case "sunny":
    print("Wear sunscreen")
    fallthrough 
    default: 
    print("Enjoy your day!")
}
// Prints - Wear sunscreen
//          Enjoy your day!

2      

how do you want the switch statement to behave? To call the default block in all cases? In that case, you have to add fallthrough in each case, excluding the default one.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

this seems to be the only chat people care about

You posted the same thing in three threads within 15 minutes of each other. You have to give people a chance to answer before assuming nobody cares.

This code aint workin'.

What does this mean? What do you expect it to do? What is it doing instead? You gotta give us something more to work with when asking a question.

Just like @vtabmow, your code worked fine for me.

3      

All I got was [Running] swift "c:\Swift\bin\Switch Statements.swift"

[Done] exited with code=0 in 6.002 seconds. I'm doing this on windows and using visual studios 2022

2      

Yeah, sorry. I'm using visual studios 2022 on windows 10 to do this. It should work but all I get is it started running and then ended.

2      

Your code is fine. So look to Visual Studio for the issue. Maybe there's a setting for displaying print output that is disabled? I really don't know what exactly it is, but the issue isn't in your code.

2      

Alright, I'll look into updates, settings, anything I can find.

2      

Sounds like your code is running, you're just not seeing the output like @roosterboy is saying. Print shows up in the debug window. Try Ctrl+G maybe.

2      

This may sound weird @vtabmow but whats ur email, I tried ctrl+G, didnt work. I wanna send you pics on what it looks like.

2      

NVM!! It worked finally, dunno what I did, if I did anything.

2      

Mine is a snip-it.

You can save those on your computer and then post using the instructions linked above. It's always best to post images to the forum unless the other person agrees to have them sent. Not everyone wants to give out their email address, twitter handle, etc.

2      

Yes thank you, I didn't know you could do it with snip it.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.