I'm currently very new to Swift and I was wondering if the following code can be optimized/shortened in any way:
enum CardOrientation {
case Horizontal, Vertical
}
func toggleCurrentCardOrientation() {
switch currentCardOrientation {
case .Horizontal: currentCardOrientation = .Vertical
case .Vertical: currentCardOrientation = .Horizontal
}
}
Let's assume that CardOrientation
will always just have these two possible values and every call to toggleCurrentCardOrientation
should toggle between each of those.
Two possible solutions:
Use a Bool instead (e.g. isCardOrientationHorizontal)
Bools are extremely easy to toggle: isCardOrientationHorizontal.toggle()
Add a toggle method to your enum:
enum CardOrientation {
case Horizontal, Vertical
mutating func toggle() {
switch self {
case .Horizontal:
self = .Vertical
case .Vertical:
self = .Horizontal
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With