Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum rawValue of color doesn't produce any color

Tags:

colors

swiftui

I am currently following Apple Developer Tutorial but i am stuck at the enum color step. My code doesn't have error i followed their code strictly here is a short code similar to my full code.

Problem here is when i call mainColor it never return any color and i always see a default color in all my view. Could you help me with this and why Apple Code give different result?

enum Theme: String {
  case bubblegum
  case buttercup
  case tan
  case teal
  case yellow

  var mainColor: Color {
    Color(rawValue)
  }
  }

if i give my view direct initialized value like .orange it works.

like image 704
Steven-Carrot Avatar asked Sep 12 '25 06:09

Steven-Carrot


2 Answers

The Color(rawValue) here is a value of String which equals to Color("color name").

Color("color name") is different from Color.colorname. You are supposed to create your own colors in the asset folder of your project.

However, I believe that at the start of every tutorial, Apple usually provides resource for the coding part, you should check it, otherwise just create your own colors.

like image 79
Steven-Carrot Avatar answered Sep 13 '25 21:09

Steven-Carrot


Because Color(rawValue) needs to have a value in Assets with the name of the rawValue.

You need a "Color Set" in "Assets" named "bubblegum", etc (case sensitive)

like image 37
lorem ipsum Avatar answered Sep 13 '25 21:09

lorem ipsum