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

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'.

   

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!

   

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.

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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.

1      

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

   

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.

   

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.

   

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

   

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.

   

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.

   

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

   

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.

   

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

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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.