Saturday, January 15, 2022
HomeiOS Developmentios - enum Colour just isn't returning any shade

ios – enum Colour just isn’t returning any shade


I’m following Apple’s tutorial for creating an iOS app utilizing Swift/SwiftUI however for some purpose my code would not work despite the fact that I adopted their directions (https://developer.apple.com/tutorials/app-dev-training/displaying-data-in-a-list)

My challenge is that I made an enum referred to as Theme for my colours, however as soon as I attempted to make use of it, the colour would not even present up. I used to be confused too as a result of the colours contained in the tutorial (bubblegum, buttercup, periwinkle, poppy, sky, tan…and so forth) aren’t even ones that exist so far as I am conscious, so I modified the colours to solely embody ones I am fairly certain mechanically exist (https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/shade/) but it surely’s nonetheless not working.

Right here is Theme.swift:

import SwiftUI

enum Theme: String {
    case cyan
    case pink
    case indigo
    case mint
    case orange
    case purple
    case teal
    case yellow
    
    var accentColor: Colour {
        swap self {
        case .orange, .teal, .yellow, .cyan, .mint, .pink: return .black
        case .indigo, .purple: return .white
        }
    }
    var mainColor: Colour {
        return Colour(rawValue)
    }
}

Right here is DailyScrum.swift:

import Basis

struct DailyScrum {
    var title: String
    var attendees: [String]
    var lengthInMinutes: Int
    var theme: Theme
}

extension DailyScrum {
    static let sampleData: [DailyScrum] =
    [
        DailyScrum(title: "Design", attendees: ["Rachel", "Daisy", "Simon"], lengthInMinutes: 10, theme: .yellow),
        DailyScrum(title: "Frontend Dev", attendees: ["Christine", "Darla", "Katie", "Euna"], lengthInMinutes: 5, theme: .orange),
        DailyScrum(title: "Backend Dev", attendees: ["Andrew", "Jacob", "Chad", "Eden", "Jen"], lengthInMinutes: 5, theme: .pink)
    ]
}

As you possibly can see, every DailyScrum has a theme shade derived from my enum.

Right here is ScrumsView.swift:

import SwiftUI

struct ScrumsView: View {
    let scrums: [DailyScrum]
    
    var physique: some View {
        Checklist {
            ForEach(scrums, id: .title) { scrum in
                CardView(scrum: scrum)
                    .listRowBackground(scrum.theme.mainColor)
            }
        }
    }
}

struct ScrumsView_Previews: PreviewProvider {
    static var previews: some View {
        ScrumsView(scrums: DailyScrum.sampleData)
    }
}

I’m attempting to loop by means of every DailyScrum and provides every one a background shade of their very own theme, however the shade would not present up and its simply white. So its like there’s a problem with scrum.theme.mainColor the place its simply not returning the colour. The identical challenge happens within the code under the place I used scrum.theme.mainColor once more but the preview simply exhibits a white background as a substitute of yellow.

CardView.swift:

import SwiftUI

struct CardView: View {
    let scrum: DailyScrum
    var physique: some View {
        VStack(alignment: .main) {
            Textual content(scrum.title)
                .font(.headline)
                .accessibilityAddTraits(.isHeader)
            Spacer()
            HStack {
                Label("(scrum.attendees.rely)", systemImage: "particular person.3")
                    .accessibilityLabel("(scrum.attendees.rely) attendees")
                Spacer()
                Label("(scrum.lengthInMinutes)", systemImage: "clock")
                    .accessibilityLabel("(scrum.lengthInMinutes) minute assembly")
                    .labelStyle(TrailingIconLabelStyle())
            }
            .font(.caption)
        }
        .padding()
        .foregroundColor(scrum.theme.accentColor)
    }
}

struct CardView_Previews: PreviewProvider {
    static var scrum = DailyScrum.sampleData[0]
    static var previews: some View {
        CardView(scrum: scrum)
            .background(scrum.theme.mainColor)
            .previewLayout(.fastened(width: 400, peak: 60))
    }
}

I do not perceive why the colour is not displaying up. I feel theres a problem inside Theme.swift with

var mainColor: Colour {
        return Colour(rawValue)
    }

RELATED ARTICLES

Most Popular

Recent Comments