im encountering an issue within my foreach loop displaying duplicate views with the same content. a report for example may contain the same strings, when leaving the view it will display the next view with the same content.
My foreach loop:
Section(header: Text("Reports")) {
ForEach(stashed.userReports, id: \.self) { report in
NavigationLink(destination: ReportView(report: report)) {
VStack(alignment: .leading) {
Text("Date")
.bold()
Text(report)
}
}
}
}
model
@Model
final class ActiveStash {
var substanceClass: String
var substance: String
var weight: UInt
var substanceCost: UInt?
var StashDescription: String?
var date: Date
var unit: String
var dosages: Int?
@Attribute(.unique) var userReports: [String]
Ive tried giving the unique attribute to my userReports array to no luck.
Bro it sounds like the problem is occurring because the userReports array treats reports with the same content as identical, even if they're different instances. This happens because userReports is an array of strings, so it doesn't differentiate between reports with the same content.
To fix this, you can change your data model to use a unique identifier for each report. Here's how you can do it:
Instead of storing strings directly in the userReports array, create a new Report struct to represent each report. This struct can include properties like the report's content and a unique identifier.
By doing this, you ensure that even if two reports have the same content, they're treated as distinct because each one has its own unique identifier. This should prevent the issue of duplicate views displaying the same content.
// Define a struct for the report
struct Report: Identifiable {
let id = UUID()
let content: String // Change this to whatever properties your report has
}
// Modify the userReports array in ActiveStash to hold Report instances
@Model
final class ActiveStash {
// Your other properties...
// Instead of [String], use [Report]
@Attribute(.unique) var userReports: [Report]
}
// Modify your ForEach loop to use the unique identifier
Section(header: Text("Reports")) {
ForEach(stashed.userReports) { report in
NavigationLink(destination: ReportView(report: report)) {
VStack(alignment: .leading) {
Text("Date")
.bold()
Text(report.content) // Access the content of the report
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With