TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Bind to a struct with a property of class type (@Model)

Forums > SwiftUI

Hello! I'm not able to find the correct way to reflect data change in my app. I'm probably mentally stuck, so a new and fresh approach could help. I have this structure that represents an entry in my view hierarchy.

struct WeekdayEntry: Identifiable, Equatable, Hashable {
    let id: UUID = UUID()
    let date: Date
    var event: Event?
}

This is the code of my view (of course it's simplified):

@State private var dataSource: [WeekdayEntry] = [] // this is filled in another part of the code, so it's never empty
@State private var isCreationMode: Bool = false

ScrollView {
    ForEach(dataSource.sorted { $0.date < $1.date }) { entry in
        WeekdayRowView(entry: entry, isCreationMode: $isCreationMode)
    }
}

This is the WeekdayRowView:

struct WeekdayRowView: View {

    @State var entry: WeekdayEntry
    .
    .
    .
}

When I enable creation mode, I'm able to change the data. Then, when I finish, I'd like to have those data changes reflected in my list, but this does not happen. Surely I need a @Binding inside the WeekdayRowView. But, how can the event reference be updated within the WeekendEntry object?

This is the visual representation of the above code. The WeekdayRowView is the one in the list below the calendar, with the grey date.

screen

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.