0

I have a variable which is @State and I receive a @Binding value from that. I want to have a variable that changes the color based on the value of my @State variable.

Example

@State var great: String = ""
var bkgColor: Color
bkgColor = Color.red         //if great == "1"
bkgColor = Color.green       // if great == "0"
bkgColor = Color.gray        // if great == "" or default

1 Answer 1

2

You can use a computed property, like:

@State var great: String = ""
var bkgColor: Color {
   switch(great) {
      case "1":
        return Color.red
      case "0":
        return Color.green
      default:
        return Color.gray
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.