Apple 的 SwiftUI 教程产生编译错误

vac*_*ead 0 xcode swiftui

我刚刚开始学习 SwiftUI,所以我决定使用最新的 Xcode (12.5) 学习 Apple 的教程。一行代码立即给了我一个语义错误:"Value of type 'Color' has no member 'accessibleFontColor'"

这是整个源模块:

//
//  CardView.swift
//  Scrumdinger
//
//  Created by Vacuumhead on 6/4/21.
//

import SwiftUI

struct CardView: View {
    let scrum: DailyScrum
    var body: some View {
        VStack(alignment: .leading) {
            Text(scrum.title).font(.headline)
            Spacer()
            HStack {
                Label("\(scrum.attendees.count)", systemImage: "person.3")
                    .accessibilityElement(children: .ignore)
                    .accessibilityLabel(Text("Attendees"))
                    .accessibilityValue(Text("\(scrum.attendees.count)"))
                Spacer()
                Label("\(scrum.lengthInMinutes)", systemImage: "clock")
                    .padding(.trailing, 20)
                    .accessibilityElement(children: .ignore)
                    .accessibilityLabel(Text("Meeting length"))
                    .accessibilityValue(Text("\(scrum.lengthInMinutes) minutes"))
            }
            .font(.caption)
        }
        .padding()
        .foregroundColor(scrum.color.accessibleFontColor)

    }
}

struct CardView_Previews: PreviewProvider {
    static var scrum = DailyScrum.data[0]
    static var previews: some View {
        CardView(scrum: scrum)
            .background(scrum.color)
            .previewLayout(.fixed(width: 400, height: 60))
    }
}
Run Code Online (Sandbox Code Playgroud)

得到错误的行是最后一行body

.foregroundColor(scrum.color.accessibleFontColor)
Run Code Online (Sandbox Code Playgroud)

消息是"Value of type 'Color' has no member 'accessibleFontColor'"

当我注释掉该行时,程序编译并运行得很好,当然没有任何颜色。我从恐龙时代就一直在写 C++,但我是 SwiftUI 的新手,甚至不知道我应该在哪里解决这个问题。

欢迎任何建议。

jn_*_*pdx 5

您可能在示例项目中缺少一个文件,该文件Color+Codable.swift定义了Color. 一种是accessibleFontColor

extension Color {
  var accesibleFontColor : Color {
    //etc.
  }
}
Run Code Online (Sandbox Code Playgroud)

https://developer.apple.com/tutorials/app-dev-training/managing-state-and-life-cycle下载文件,并确保您Color+Codable.swift在项目中使用。