GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Can you guys help me understand this syntax? Please

Forums > SwiftUI

Hi guys, I'm completely new to SwiftUI and I'm following the official apple tutorial (I'm specifically in this section right now: [https://developer.apple.com/tutorials/app-dev-training/creating-a-card-view]). But, I do not understand this syntax:

import SwiftUI

enum Theme: String {
    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
        }
    }
    var mainColor: Color {
        Color(rawValue)
    }
}

Specifically the "return .white" and "return .black" code. Why are we returning .white and .black?

They look like enum cases or something but the "var accentColor" says it will return a "Color" type, so why are we not returning "Color()"? instead of just .white and .black?

Thanks in advance and have a good day you all!

2      

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
        }
    }
}

2      

Thank you very much! it's very clear now! This case is like when you have a variable of type SomeEnum and for the value assignment you only have to write .case1, .case2 instead of SomeEnum.case1. Again, Thank you!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.