Hi! The below is computed property.
var accentColor: Color {
switch self {
case .bubblegum, .buttercup, .lavender, .orange, .periwinkle, .poppy, .seafoam, .sky, .tan, .teal, .yellow: return .black
case .indigo, .magenta, .navy, .oxblood, .purple: return .white
}
}
depending on the value of enum. it will return you the color you can use. For .bubblegum
it will be white color and for .indigo
it will be black color etc. The accentColor
is of type Color. So compiler can infer that and you don't need to write your return value as Color.white or Color.black
though you can if you wish.
To understand better why you might need that just copy and have a look how it works.
struct ContentView: View {
@State private var background = Theme.bubblegum
let options = Theme.allCases
var body: some View {
ZStack {
background.accentColor
Picker("Options", selection: $background) {
ForEach(options, id: \.self) { option in
Text(option.rawValue.capitalized)
}
}
}
.ignoresSafeArea()
}
}
enum Theme: String, CaseIterable {
case bubblegum
case buttercup
case indigo
case lavender
case magenta
case navy
case orange
case oxblood
case periwinkle
case poppy
case purple
case seafoam
case sky
case tan
case teal
case yellow
var accentColor: Color {
switch self {
case .bubblegum, .buttercup, .lavender, .orange, .periwinkle, .poppy, .seafoam, .sky, .tan, .teal, .yellow: return .black
case .indigo, .magenta, .navy, .oxblood, .purple: return .white
}
}
}