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

SOLVED: Day 5 - Cheking multiple conditions. Inquiry on printing out the statement when I change the enum value.

Forums > 100 Days of SwiftUI

So one the example of checking multiple conditions with enums the code is laid out like this. Mind you that I tweek the code al little bit as to what it's supposed to print out. Nothing minor.


enum transportOption { case airplane, helicopter, bicycle, car, scooter } var transportation = transportOption.car

if transportation == .airplane || transportation == .helicopter { print("Let's fly away like an eagle") } else if transportation == .car{ print("Let's get rolling") } else { print("I feel like I want to be Eco-friendly") }

transportation = .car


Now this print out the code as it's suppose to as long as I change the var "transportation " below the enum syntax. The question that I have is how can I change the print out message so I can get something else printed out when I change the var of transportation down the road lets say xxx of lines further. I still get the original message for .bicicle || scooter in this case.

I went over the notes on Enums and I took this code from the enum lesson


enum weekday2{ case monday, tuesday, wednesday, thursday, friday } var dayOfTheWeek2 = weekday2.friday dayOfTheWeek2 = .monday print(dayOfTheWeek2)

dayOfTheWeek2 = .tuesday print(dayOfTheWeek2)


when I run the code above i can change the var "dayOfTheWeek" successfully xxx amount of lines in the future and it prints out

1      

When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

I have reformatted you code snippets for better display.

Snippet #1:

enum transportOption { 
    case airplane, helicopter, bicycle, car, scooter
} 

var transportation = transportOption.car

if transportation == .airplane || transportation == .helicopter {
    print("Let's fly away like an eagle")
} else if transportation == .car {
    print("Let's get rolling")
} else {
    print("I feel like I want to be Eco-friendly")
}

transportation = .car

Snippet #2:

enum weekday2 {
    case monday, tuesday, wednesday, thursday, friday
}

var dayOfTheWeek2 = weekday2.friday
dayOfTheWeek2 = .monday
print(dayOfTheWeek2)

dayOfTheWeek2 = .tuesday
print(dayOfTheWeek2)

Now, as to your actual question...

I assume you are doing this in a Playground. Playgrounds execute lines of code from top to bottom in order. So the only way to print "Let's get rolling" after changing transportation to .car would be to duplicate the if... else clause above.

Except that enums can contain methods and computed properties, so you could add this to your transportOption enum:

var message: String {
    switch self {
        case .airplane, .helicopter:
            return "Let's fly away like an eagle"
        case .car:
            return "Let's get rolling"
        default:
            return "I feel like I want to be Eco-friendly"
    }
}

This returns a String message depending on what the value of the variable is. You would use it like so:

var transportation = transportOption.car
print(transportation.message)
//Let's get rolling
transportation = transportOption.helicopter
print(transportation.message)
//Let's fly away like an eagle

Side note: The convention in Swift is to name types starting with a capital letter and variables starting with a lowercase letter. So you should use TransportOption and Weekday2 instead of transportOption and weekday2.

2      

@roosterboy thank you for the clarification. Later while learning about switch statements I was thinking that was going ot be the right way of doing it.

Thank you for the clarification . If i put the 3x tics will my it snippet it like you did when I paste it to the forum . I also see a code button.

I also want thank you for the info on how to properly syntax the convention name.

var forecast2 = weather.rain

switch forecast{
case    .sun:
    print("It should be a rainny day")

case    .rain:
    print("pack an Umbrella")

case    .wind:
    print("It's Windy")

case    .snow:
    print("School is cancelled")

case    .unknown:
    print("Our forecast Generator is Broken")
}

Okay there you go Learned how to Snippet my code

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.