An Interest In:
Web News this Week
- April 29, 2024
- April 28, 2024
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
April 5, 2023 12:49 pm GMT
Original Link: https://dev.to/arnosolo/enum-of-structs-in-swift-4if5
Enum of structs in Swift
So I want to store color setup locally, but it would be better if I only save a String value. Then use this string to get the corresponding value. Below is my result:
// MyColor.swiftclass MyColor { private init() {} static let primary = Color("primary") static let onPrimary = Color("onPrimary") static let secondary = Color("secondary") static let onSecondary = Color("onSecondary")}struct ColorPair { let bg: Color let onBg: Color}enum ColorPairType: String { case primary = "primary" case secondary = "secondary" func value() -> ColorPair { switch self { case .primary: return ColorPair(bg: MyColor.primary, onBg: MyColor.onPrimary) case .secondary: return ColorPair(bg: MyColor.secondary, onBg: MyColor.onSecondary) }}
// DemoView.swift@State var currentColor: ColorPairType = .secondaryText("hello") .foregroundColor(currentColor.value().onBg)
If you find this article useful, maybe you can buy my calculator for $0.99? It's a calculator that can change the layout of the keys. This way you can only keep the keys that are useful to you. -> App Store
Original Link: https://dev.to/arnosolo/enum-of-structs-in-swift-4if5
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To