When trying to update view faced an error "property defined on _CA Layer View"

192 views Asked by At

I am new to Swift and trying to make a first app on SwiftUI on developer.apple and faced a problem when trying to update a view. I was trying to find a mistake, but I did everything step by step and code is same with sample code. I can not understand where is mistake. Hope someone can help me.

Error when add this line of code

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'

Error when typing 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)
                                }
                                
                            }
                        }
                }
            }
        }
1

There are 1 answers

0
milban On BEST ANSWER

My guess is that the 'DailyScrum.swift' file is probably missing some code. Fix DailyScrum.swift file like the following code.

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