The below code when initialized with a raw value returns NamedColor(name: “colorName” bundle:nil) with colourName being the name of the case in the enum and the raw value being passed in being the sting representation of the colour eg “oxblood” when trying to access mainColor. All colours are currently present in my assets folder.
Theme Enum
enum Theme: String, Codable, CaseIterable, Identifiable {
case bubblegum
case buttercup
case indigo
case lavender
case magentas
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, .magentas, .navy, .oxblood, .purple: return .white
}
}
var mainColor: Color {
return Color(rawValue)
}
var name: String{
rawValue.capitalized
}
var id: String{
name
}
}
I am looking for this to instead return just the color so that it can be used in my views when accessing the mainColor variable. Why is it not working?
2
change based on below code it worked for me
import SwiftUI
enum Theme: String, Codable, CaseIterable, Identifiable {
case bubblegum
case buttercup
case indigo
case lavender
case magentas
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, .magentas, .navy, .oxblood, .purple: return .white
}
}
var mainColor: Color {
Color(rawValue)
}
var name: String{
rawValue.capitalized
}
var id: String{
name
}
}
struct ContentView: View {
var orangeColorTheme = Theme(rawValue: "orange")
var purpleColorTheme = Theme(rawValue: "purple")
var body: some View {
VStack {
Text("Orange Theme")
.background(orangeColorTheme?.mainColor)
Text("Purple Theme")
.background(purpleColorTheme?.mainColor)
}
}
}