GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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+

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!

Learn more here

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.