Home > Blockchain >  Stuck in the SwiftUI Tutorial by Apple in Chapter 5 (Updating App data)
Stuck in the SwiftUI Tutorial by Apple in Chapter 5 (Updating App data)

Time:08-06

import SwiftUI

struct ScrumsView: View {
    @Binding var scrums: [DailyScrum]
    @State private var isPresentingNewScrumView = false
    @State private var newScrumData = DailyScrum.Data()
    
    var body: some View {
        List {
            ForEach($scrums) { $scrum in
                NavigationLink(destination: DetailView(scrum: $scrum)) {
                    CardView(scrum: scrum)
                }
                .listRowBackground(scrum.theme.mainColor)
            }
        }
        .navigationTitle("Daily Scrums")
        .toolbar {
            Button(action: {
                isPresentingNewScrumView = true
            }) {
                Image(systemName: "plus")
            }
            .accessibilityLabel("New Scrum")
        }
        .sheet(isPresented: $isPresentingNewScrumView) {
            NavigationView {
                DetailEditView(data: $newScrumData)
                    .toolbar {
                        ToolbarItem(placement: .cancellationAction) {
                            Button("Dismiss") {
                                isPresentingNewScrumView = false
                                newScrumData = DailyScrum.Data()
                            }
                        }
                        ToolbarItem(placement: .confirmationAction) {
                            Button("Add") {
                                
                                
                                
                                
                                let newScrum = DailyScrum(data: newScrumData)
                                scrums.append(newScrum)
                                isPresentingNewScrumView = false
                                newScrumData = DailyScrum.Data()
                                
                             
                                
                            }
                        }
                    }
            }
        }
    }
}

struct ScrumsView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            ScrumsView(scrums: .constant(DailyScrum.sampleData))
        }
    }
}

This is the ScrumsView and I get 2 Erros in Line let newScrum = DailyScrum(data: newScrumData). ItÄs in the Button "Add".

It says: "Extra argument 'data' in call" & "Missing arguments for parameters 'title', 'attendees', 'lengthInMinutes', 'theme' in call"

I followed each step of the tutorial and I even downloaded the completed file of the next chapter but that didn't help me either. I looked in forums and I found that there was a func missing in the tutorial in the extension DailyScrum, but even after I added this func it still didn't work. Here is the DailyScrum File for context:

import Foundation

struct DailyScrum: Identifiable {
    let id: UUID
    var title: String
    var attendees: [Attendee]
    var lengthInMinutes: Int
    var theme: Theme
    
    init(id: UUID = UUID(), title: String, attendees: [String], lengthInMinutes: Int, theme: Theme) {
        self.id = id
        self.title = title
        self.attendees = attendees.map { Attendee(name: $0) }
        self.lengthInMinutes = lengthInMinutes
        self.theme = theme
    }
}

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)
  }
   
  mutating func update(from data: Data) {
    title = data.title
    attendees = data.attendees
    lengthInMinutes = Int(data.lengthInMinutes)
    theme = data.theme
  }
}


extension DailyScrum {
    static let sampleData: [DailyScrum] =
    [
        DailyScrum(title: "Design", attendees: ["Cathy", "Daisy", "Simon", "Jonathan"], lengthInMinutes: 10, theme: .yellow),
        DailyScrum(title: "App Dev", attendees: ["Katie", "Gray", "Euna", "Luis", "Darla"], lengthInMinutes: 5, theme: .orange),
        DailyScrum(title: "Web Dev", attendees: ["Chella", "Chris", "Christina", "Eden", "Karla", "Lindsey", "Aga", "Chad", "Jenn", "Sarah"], lengthInMinutes: 5, theme: .poppy)
    ]
}

Does someone have a solution for this?

CodePudding user response:

In your ScrumsView, in the second ToolbarItem, you declared this:

 let newScrum = DailyScrum(data: newScrumData)

DailyScrum does not have initializer that takes (data: ), so results in this error:

it says: "Extra argument 'data' in call" & "Missing arguments for parameters 'title', 'attendees', 'lengthInMinutes', 'theme' in call"

Solution: Add an init(data: Data) in your DailyScrum struct.

struct DailyScrum: Identifiable {

  ......

  init(data: Data) { //add this
    self.id = UUID()
    self.title = data.title
    self.attendees = data.attendees
    self.lengthInMinutes = Int(data.lengthInMinutes)
    self.theme = data.theme
  }
}

CodePudding user response:

If you look carefully into Section 2 Step 3, you will notice they have added a new initializer for DailyScrum right after the update function:

init(data: Data) {
    id = UUID()
    title = data.title
    attendees = data.attendees
    lengthInMinutes = Int(data.lengthInMinutes)
    theme = data.theme
}

In fact, if you download the project files for Updating App Data, you will the initializer was already there in the StartingProject folder.


In conclusion, when you follow the tutorial, you should always download the project file, and start working with the StartingProject folder to guarantee everything behaves the same as described in the tutorial.

  • Related