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

How to use #Preview

Forums > SwiftUI

I don't know how to fix this. I don't know how to code #preview part of EventView. Here is my code:

import SwiftUI

struct AddEvent: Codable, Identifiable {
    var id = UUID()
    let eventName: String
    let amount: Int
}

@Observable
class Events {
    var items = [AddEvent]() {
        didSet {
            if let encoded = try? JSONEncoder().encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }

    init() {
        if let savedItems = UserDefaults.standard.data(forKey: "Items") {
            if let decodedItems = try? JSONDecoder().decode([AddEvent].self, from: savedItems) {
                items = decodedItems
                return
            }
        }

        items = []
    }
}

let students = Bundle.main.decode("students.json")

struct ContentView: View {
    @State var events = Events()
    @State private var showingAddEvents = false

    var body: some View {
        NavigationStack {
            List {
                ForEach(events.items) { event in
                    NavigationLink {
                        EventView(event: event)
                    } label: {
                        HStack {
                            VStack {
                                Text(event.eventName)
                                Text("$ \(event.amount)")
                            }
                            Spacer()
                            Text("多少人沒繳錢")
                        }
                    }
                }
            }
            .navigationTitle("Payment Registration")
            .toolbar {
                Button("show add event", systemImage: "plus") {
                    showingAddEvents = true
                }
            }
            .sheet(isPresented: $showingAddEvents) {
                AddEventView(events: events)
            }
        }
    }
}

#Preview {
    ContentView()
}
import SwiftUI

struct EventView: View {
    let event: AddEvent

    var body: some View {
        let thisEventName = event.eventName

        NavigationStack {
            ForEach(students) {student in
                Text(student.name)
            }
            .navigationTitle(thisEventName)
        }
    }
}

#Preview {
    EventView()
}

1      

Add

#Preview {
    EventView()
      .environment(Events())
}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

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.