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
.