Creating A Card View Tutorial Theme Issue

Hi all,

I'm following this tutorial to the letter -- https://developer.apple.com/tutorials/app-dev-training/creating-a-card-view#Create-a-Color-Theme -- however, I cannot get the theme colors to change. I've tracked down the issue to this line of code --

var mainColor: Color {
        Color(rawValue)
    }

-- which is in the Theme.swift file I am asked to create. I have tripled checked the syntax, references, and such.

And it works when I pass it a literal value like this:

var mainColor: Color {
        Color(.yellow)
    }

Here is my entire Theme.swift file:

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

And this is what is used in the tutorial.

Is there something I'm missing? I'm using macOS Big Sur 11.6.2 and Xcode 13.2.1.

Thanks!

Answered by OOPer in 700841022

Is there something I'm missing?

The code you have shown looks correct.

One possibility, have you started your project with StartingProject? (Included in the Project download link.)

You need Assets.xcassets to make the code work.

Accepted Answer

Is there something I'm missing?

The code you have shown looks correct.

One possibility, have you started your project with StartingProject? (Included in the Project download link.)

You need Assets.xcassets to make the code work.

10

That was it! I was looking there before, but didn't realize I was missing that Themes component. I started from scratch and didn't use the StartingProject. I figured starting from scratch would have been fine. Thank you very much!

I imported the theme folder and that seemed to fix my problem. I am also having a problem with the CardView swift i get this error message. "Cannot infer contextual base in reference to member 'trailingIcon'"

Here is my code. // // CardView.swift // Scrumdinger // // Created by Leonard Assenberg on 6/6/22. //

import SwiftUI

struct CardView: View {   let scrum: DailyScrum   var body: some View {     VStack(alignment: .leading){       Text(scrum.title)         .font(.headline)         .accessibilityAddTraits(.isHeader)               Spacer()       HStack {         Label("(scrum.attendees.count)", systemImage: "person.3")           .accessibilityLabel("(scrum.attendees.count) attendees")         Spacer()         Label("(scrum.lengthInMinutes)", systemImage: "clock")           .accessibilityLabel("(scrum.lengthInMinutes) minute meeting")           .labelStyle(.trailingIcon) "Cannot infer contextual base in reference to member 'trailingIcon'"       }       .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(.fixed(width: 400, height: 60))   } } I am assuming the problem is in my TrailingIconLabelStyle.swift. But the code looks good to me.

import SwiftUI

struct TrailingIconLabelStyle: LabelStyle {   func makeBody(configuration: Configuration) -> some View {     HStack {       configuration.title       configuration.icon     }   } }

extension LabelStyle where Self == TrailingIconLabelStyle {   static var trailingIcon: Self { Self() } } Any Suggestions?

I imported the theme folder and that seemed to fix my problem. I am also having a problem with the CardView swift i get this error message. "Cannot infer contextual base in reference to member 'trailingIcon'"

Here is my code. // // CardView.swift // Scrumdinger // // Created by Leonard Assenberg on 6/6/22. //

import SwiftUI
struct CardView: View {  let scrum: DailyScrum  var body: some View {   
 VStack(alignment: .leading){      
Text(scrum.title)        .font(.headline)       
 .accessibilityAddTraits(.isHeader)            
 Spacer()      
HStack {        Label("(scrum.attendees.count)", systemImage: "person.3")          .accessibilityLabel("(scrum.attendees.count) attendees")       
 Spacer()        
Label("(scrum.lengthInMinutes)", systemImage: "clock")       
   .accessibilityLabel("(scrum.lengthInMinutes) minute meeting")        
  .labelStyle(.trailingIcon) "Cannot infer contextual base in reference to member 'trailingIcon'"   
   }      .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(.fixed(width: 400, height: 60))  } } 

I am assuming the problem is in my TrailingIconLabelStyle.swift. But the code looks good to me.

import SwiftUI struct TrailingIconLabelStyle: LabelStyle {   func makeBody(configuration: Configuration) -> some View {     HStack {      configuration.title      configuration.icon    }  } } extension LabelStyle where Self == TrailingIconLabelStyle {   static var trailingIcon: Self { Self() } } Any Suggestions?

Creating A Card View Tutorial Theme Issue
 
 
Q