尝试更新视图时遇到错误 "property defined on _CA Layer View"
When trying to update view faced an error "property defined on _CA Layer View"
我是 Swift 的新手,正在尝试在 SwiftUI 和 developer.apple 上制作第一个应用程序,但在尝试更新视图时遇到了问题。我试图找出一个错误,但我一步一步地做了所有事情,代码与示例代码相同。我不明白哪里错了。希望有人能帮助我。
添加这行代码时出错
scrum.update(from: data)
*Argument passed to call that takes no arguments
*Cannot use mutating member on immutable value: 'self' is immutable
*Referencing instance method 'update()' requires wrapper 'Binding'
键入 scrum.update 时出错:
*This property is defined on _CALayerView
List {
Section(header: Text("Meeting info")) {
NavigationLink (destination: MeetingView()) {
Label("Start Meeting", systemImage: "timer")
.font(.headline)
.foregroundColor(.accentColor)
}
HStack {
Label("Length", systemImage: "clock")
Spacer()
Text("\(scrum.lengthInMinutes) minutes")
}
.accessibilityElement(children: .combine)
HStack {
Label("Theme", systemImage: "paintpalette")
Spacer()
Text(scrum.theme.name)
.padding(4)
.foregroundColor(scrum.theme.accentColor)
.background(scrum.theme.mainColor)
.cornerRadius(4)
}
.accessibilityElement(children: .combine)
}
Section(header: Text("Attendees")) {
ForEach (scrum.attendees) { attendee in
Label(attendee.name, systemImage: "person")
}
}
.navigationTitle(scrum.title)
.toolbar {
Button ("Edit") {
isPresentingEditView = true
data = scrum.data
}
}
.sheet(isPresented: $isPresentingEditView) {
NavigationView{
DetailEditView(data: $data)
.navigationTitle(scrum.title)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button ("Cancel") {
isPresentingEditView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button ("Done") {
isPresentingEditView = false
scrum.update(from: data)
}
}
}
}
}
}
我的猜测是 'DailyScrum.swift' 文件可能缺少一些代码。
像下面的代码一样修复 DailyScrum.swift
文件。
extension DailyScrum {
struct Attendee: Identifiable {
let id: UUID
var name: String
init(id: UUID = UUID(), name: String) {
self.id = id
self.name = name
}
}
struct Data {
var title: String = ""
var attendees: [Attendee] = []
var lengthInMinutes: Double = 5
var theme: Theme = .seafoam
}
var data: Data {
Data(title: title, attendees: attendees, lengthInMinutes: Double(lengthInMinutes), theme: theme)
}
// Add this code
mutating func update(from data: Data) {
self.title = data.title
self.attendees = data.attendees
self.lengthInMinutes = Int(data.lengthInMinutes)
self.theme = data.theme
}
}
我是 Swift 的新手,正在尝试在 SwiftUI 和 developer.apple 上制作第一个应用程序,但在尝试更新视图时遇到了问题。我试图找出一个错误,但我一步一步地做了所有事情,代码与示例代码相同。我不明白哪里错了。希望有人能帮助我。
添加这行代码时出错
scrum.update(from: data)
*Argument passed to call that takes no arguments
*Cannot use mutating member on immutable value: 'self' is immutable
*Referencing instance method 'update()' requires wrapper 'Binding'
键入 scrum.update 时出错:
*This property is defined on _CALayerView
List {
Section(header: Text("Meeting info")) {
NavigationLink (destination: MeetingView()) {
Label("Start Meeting", systemImage: "timer")
.font(.headline)
.foregroundColor(.accentColor)
}
HStack {
Label("Length", systemImage: "clock")
Spacer()
Text("\(scrum.lengthInMinutes) minutes")
}
.accessibilityElement(children: .combine)
HStack {
Label("Theme", systemImage: "paintpalette")
Spacer()
Text(scrum.theme.name)
.padding(4)
.foregroundColor(scrum.theme.accentColor)
.background(scrum.theme.mainColor)
.cornerRadius(4)
}
.accessibilityElement(children: .combine)
}
Section(header: Text("Attendees")) {
ForEach (scrum.attendees) { attendee in
Label(attendee.name, systemImage: "person")
}
}
.navigationTitle(scrum.title)
.toolbar {
Button ("Edit") {
isPresentingEditView = true
data = scrum.data
}
}
.sheet(isPresented: $isPresentingEditView) {
NavigationView{
DetailEditView(data: $data)
.navigationTitle(scrum.title)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button ("Cancel") {
isPresentingEditView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button ("Done") {
isPresentingEditView = false
scrum.update(from: data)
}
}
}
}
}
}
我的猜测是 'DailyScrum.swift' 文件可能缺少一些代码。
像下面的代码一样修复 DailyScrum.swift
文件。
extension DailyScrum {
struct Attendee: Identifiable {
let id: UUID
var name: String
init(id: UUID = UUID(), name: String) {
self.id = id
self.name = name
}
}
struct Data {
var title: String = ""
var attendees: [Attendee] = []
var lengthInMinutes: Double = 5
var theme: Theme = .seafoam
}
var data: Data {
Data(title: title, attendees: attendees, lengthInMinutes: Double(lengthInMinutes), theme: theme)
}
// Add this code
mutating func update(from data: Data) {
self.title = data.title
self.attendees = data.attendees
self.lengthInMinutes = Int(data.lengthInMinutes)
self.theme = data.theme
}
}