调用中缺少参数“studyCard”的参数

时间:2021-06-12 21:13:29

标签: swift database swiftui

我想调用一个名为“CardViewModel”的 ViewModel 这是我的 CardViewModel 的代码:

import SwiftUI
import Combine

final class CardViewModel: ObservableObject, Identifiable {
   private let cardRepository = CardRepository()
   @Published var studyCard: StudyCard

   var id = ""

   private var cancellables: Set<AnyCancellable> = []

   init(studyCard: StudyCard) {
       self.studyCard = studyCard
       $studyCard
           .compactMap { $0.id }
           .assign(to: \.id, on : self)
           .store(in: &cancellables)
   }
}

我制作了一个“CardListView”,其中包含一个需要这个“CardViewModel”的“添加”按钮:

Button(action: {
                    showingForm = true
                    
                    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                       if success {
                           print("User Accepted")
                       } else if let error = error {
                           print(error.localizedDescription)
                      }
                    }
                    let content = UNMutableNotificationContent()
                    content.title = "Daily Notification"
                    content.body = "Daily Notification is Ready"
                    content.sound = UNNotificationSound.default 
                    var dateComponents = cardViewModel.studyCard.date
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "dd MMMM yyyy HH:mm"
                    let date = dateFormatter.date(from: dateComponents)
                    let calendar = Calendar.current

                    let hour = calendar.component(.hour, from: date!)
                    let minutes = calendar.component(.minute, from: date!)
                
                    
                    var dateComponent = DateComponents()
                    dateComponent.hour = hour
                    dateComponent.minute = minutes
                    
                    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
                    
                })

为了在我的 CardListView 中有 CardViewModel,我这样称呼它:var cardViewModel:CardViewModel

我的问题是,当我想像这样调用 CardListView 时:CardListView(cardListViewModel:CardListViewModel()) 我有这个错误 Missing argument for parameter 'cardViewModel' in call 谢谢你的回答!

1 个答案:

答案 0 :(得分:0)

Lorem Ipsum,我会给你完整的 CardListView 脚本,因为我不明白你的解决方案:

import SwiftUI
import FirebaseFirestore
import FirebaseAuth


struct CardListView: View {
    @ObservedObject var cardListViewModel:CardListViewModel
    @State private var showingForm = false
    @State private var showPassed = false

    var cardViewModel:CardViewModel

    var body: some View {
    
    NavigationView {
        VStack {
            Toggle(isOn: $showPassed, label: {
                Text("\(showPassed ? "Cacher ":"Montrer ")les rappels terminés")
            })
            List {
                ForEach(cardListViewModel.cardViewModels.filter{
                    $0.studyCard.passed == showPassed
                }) {
                    cardVM in CardView(cardViewModel: cardVM)
                        .onTapGesture(count: 2, perform: {
                            var studyCard = cardVM.studyCard
                            studyCard.passed.toggle()
                            cardListViewModel.update(studyCard)
                            
                        })
                        
                            
                        
                }.onDelete(perform: delete)
            }
            .listStyle(InsetListStyle())
            .navigationTitle("Rappels")

            HStack {
                Button(action: {
                    showingForm = true
                    
                    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                       if success {
                           print("User Accepted")
                       } else if let error = error {
                           print(error.localizedDescription)
                      }
                    }
                    let content = UNMutableNotificationContent()
                    content.title = "Daily Notification"
                    content.body = "Daily Notification is Ready"
                    content.sound = UNNotificationSound.default //you can play with it
                    
                    var dateComponents = cardViewModel.studyCard.date
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "dd MMMM yyyy HH:mm"
                    let date = dateFormatter.date(from: dateComponents)
                    let calendar = Calendar.current

                    let hour = calendar.component(.hour, from: date!)
                    let minutes = calendar.component(.minute, from: date!)
                
                    
                    var dateComponent = DateComponents()
                    dateComponent.hour = hour
                    dateComponent.minute = minutes
                    
                    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
                    
                }) {
                    Circle()
                        .fill(Color.green)
                        .frame(height:60)
                        .overlay(Image(systemName: "plus").foregroundColor(.white))
                }.sheet(isPresented: $showingForm) {
                    FormView { (studyCard) in
                        cardListViewModel.add(studyCard)
                        showingForm = false
                    }
                }
                Button(action: {
                    
                    try! Auth.auth().signOut()
                    UserDefaults.standard.set(false, forKey: "status")
                    NotificationCenter.default.post(name: NSNotification.Name("status"), object: nil)
                    
                }) {
                    
                    Text("Log out")
                        .foregroundColor(.white)
                        .padding(.vertical)
                        .frame(width: UIScreen.main.bounds.width - 50)
                }
                .background(Color("Color"))
                .cornerRadius(10)
                .padding(.top, 25)
            }
        }
    }
}
private func delete(at offsets:IndexSet) {
    offsets.map{ cardListViewModel.cardViewModels[$0].studyCard}.forEach(cardListViewModel.remove)
}

}