dimanche 22 mars 2020

How to use if statement with struct in SwiftUI?

I am trying to use an if statement with a bool value in a struct to then see if a content view should be shown using that data. I keep getting the error "Ambiguous reference to member 'indices'" but I know this is not the actual problem it is just swiftUI saying there's an error on the forEach when it is the thing inside the forEach. This is the struct definition where needsToBeDeleted is false by default because it should be shown when first created

import Foundation

struct SEvent {
    var eventName:String;
    var timeSpent:String;
    var timeStarted:Date;
    var lastStarted:Date?;
    var needsToBeDeleted = false;
}

and this is the piece where I am iterating over an array of the events

ForEach(arrayOfEvents.indices, id: \.self) { index in
  if !self.arrayOfEvents[index].needsToBeDeleted {
     EventContentView(eventName: self.arrayOfEvents[index].eventName, timeSpent: 
     self.arrayOfEvents[index].timeSpent, shouldDelete: 
     self.$arrayOfEvents[index].needsToBeDeleted)
  }

}

Also this is the array declaration if that helps

@State private var arrayOfEvents = [SEvent]();

Aucun commentaire:

Enregistrer un commentaire