UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI generates a new UUID for my Structs

Forums > SwiftUI

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.

   

Why do you have var in your struct for the phrase and author? Are you planning to change the phrase or author or both details, once you have set up your Reminder array?

I can imagine that a phrase is only attributable to one author. Anyway, these could be let rather than var.

For a similar reason, unless you plan to have changeable 'id's then this should also be a let.

struct Reminder: Codable, Identifiable, Equatable, Hashable{
    let id = UUID()
    let phrase: String
    let author: String

    init(phrase: String, author: String) {
        self.phrase = phrase
        self.author = author
        self.phrase.addQuotationMarks()
    }
}

This will cause a XCode warning for the 'id', so you need to specify what are the coding keys for the Codable, so try adding CodingKeys like this

struct Reminder: Codable, Identifiable, Equatable, Hashable{
    let id = UUID()
    let phrase: String
    let author: String

    init(phrase: String, author: String) {
        self.phrase = phrase
        self.author = author
        self.phrase.addQuotationMarks()
    }

    enum CodingKeys: String, CodingKey {
        case phrase
        case author
    }
}

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.