0

Let say I have a SwiftUI component and I want it to change color according to a parameter I choose like :

Component(colorValue: "1")
Component(colorValue: "2")
Component(colorValue: "3")

colorValue: 1 would change the color of my component to red, 2 to green, 3 to blue for example.

When I only had 2 colors in the beggining of my project I used a boolean in my component :

Rectangle().fill(Color(colorValue ? "red" : "blue"))

But now that I have multiple choice, how can I achieve the color change by checking the value of the colorValue String ?

Thanks in advance !

1 Answer 1

1

just use a function

func getColorValue() -> Color {
  if colorValue == 1 { return .red }
  // and so on
}

Rectangle().fill(Color(getColorValue()))
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.