Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 5, 2023 12:49 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To