MortySmith@programming.dev to Swift@programming.dev · 4 months agoIf you have more than 2 conditions, and do not want to recreate an element with if else statement, what do you do since ternary operations might get too confusing?programming.devimagemessage-square3fedilinkarrow-up19arrow-down10
arrow-up19arrow-down1imageIf you have more than 2 conditions, and do not want to recreate an element with if else statement, what do you do since ternary operations might get too confusing?programming.devMortySmith@programming.dev to Swift@programming.dev · 4 months agomessage-square3fedilink
minus-squareSjmarf@sh.itjust.workslinkfedilinkarrow-up3·edit-24 months agoOne option would be to use an enum with a label computed property. enum TransitionState { case stageOne, stageTwo, stageThree var label: String { switch self { case .stageOne: "Stage one!" case .stageTwo: "Stage two!" case .stageThree: "Stage three!" } } } struct MyView: View { @State var transitionState: TransitionState = .stageOne var body: some View { Text(transitionState.label) } }
One option would be to use an enum with a
label
computed property.enum TransitionState { case stageOne, stageTwo, stageThree var label: String { switch self { case .stageOne: "Stage one!" case .stageTwo: "Stage two!" case .stageThree: "Stage three!" } } }
struct MyView: View { @State var transitionState: TransitionState = .stageOne var body: some View { Text(transitionState.label) } }