I want to build an App that stores Reminders (e.g Quotes) and displays it on the ContentView. You can create own reminders or get them from another View that has an Array of Reminders stored that I manually typed in. But every time I add one from my ExploreView to the array that is on the ContentView I append a Struct with the UUID() of the Element from the Explore View. But when I go back into my exploreView from my ContentView all Elements have a new UUID
I'm also curious why that generates new UUID() but the UUID() from the stored Elements in my ContentView stay the same.
struct Reminder: Codable, Identifiable, Equatable, Hashable{
var id = UUID()
var phrase: String
var author: String
init(phrase: String, author: String) {
self.phrase = phrase
self.author = author
self.phrase.addQuotationMarks()
}
}
class ExploreReminders {
let exploreReminders: [Reminder] = [
Reminder(phrase: "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.", author: "Albert Einstein"),
Reminder(phrase: "The only way to do great work is to love what you do.", author: "Steve Jobs"),
Reminder(phrase: "Life is what happens when you're busy making other plans.", author: "John Lennon"),
Reminder(phrase: "In three words I can sum up everything I've learned about life: it goes on.", author: "Robert Frost"),
Reminder(phrase: "You only live once, but if you do it right, once is enough.", author: "Mae West"),
Reminder(phrase: "Yesterday is history, tomorrow is a mystery, today is a gift of God, which is why we call it the present.", author: "Bil Keane")
]
}
import SwiftUI
struct ExploreView: View {
@State var exploreReminders = ExploreReminders()
@State var allReminders: AllReminder
@State private var userSearch = ""
var searchResults: [Reminder] {
if userSearch.isEmpty{
return exploreReminders.exploreReminders
} else{
let editedUserSearch = userSearch.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
let searchResult = exploreReminders.exploreReminders.filter {
let editedPhrase = $0.phrase.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
let editedAuthor = $0.author.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
return editedPhrase.contains(editedUserSearch) || editedAuthor.contains(editedUserSearch)
}
return searchResult
}
}
var body: some View {
NavigationStack {
ScrollView{
LazyVGrid(columns:[
GridItem(.flexible(minimum: 100, maximum: 200), spacing: 20),
GridItem(.flexible(minimum: 100, maximum: 200), spacing: 20)
], spacing: 20) {
ForEach(searchResults) { reminder in
NavigationLink{
ReminderDetailView(reminder: reminder, allReminders: allReminders)
} label:{
ReminderView(reminder: reminder, allReminders: allReminders)
.aspectRatio(1, contentMode: .fit)
}
}
}
.padding()
}
.navigationTitle("Explore")
.searchable(text: $userSearch, placement: .navigationBarDrawer(displayMode: .always), prompt: "Search for Phrases or Authors")
}
}
I tried to outsource the ExploreReminderArray to a outer class as you can see but that still didn't work.