首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >苹果的SwiftUI教程产生了编译错误

苹果的SwiftUI教程产生了编译错误
EN

Stack Overflow用户
提问于 2021-06-04 19:58:47
回答 1查看 449关注 0票数 0

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

下面是整个源模块:

代码语言:javascript
复制
//
//  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))
    }
}

获得错误的行是body的最后一行。

代码语言:javascript
复制
.foregroundColor(scrum.color.accessibleFontColor)

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

当我注释掉这一行时,这个程序编译并运行得很好,当然没有任何颜色。从恐龙时代起,我就开始写C++了,但我对SwiftUI并不熟悉,甚至不知道该从哪里着手解决这个问题。

欢迎任何建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-04 20:06:17

示例项目中可能缺少一个名为Color+Codable.swift的文件,该文件定义了Color上的一些扩展名。一个是accessibleFontColor

代码语言:javascript
复制
extension Color {
  var accesibleFontColor : Color {
    //etc.
  }
}

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67843313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档