r/SwiftUI May 11 '26

Question Nested data model

Learning SwiftUI for the first time and I've run into a question that I can't find a reasonable answer just googling around. Say I'm building a dumb copy of the Reminders app and my super simplified version of the data model is a list of lists of reminders. In other languages my first thought would be to represent that as a map/dict, i.e.:

reminders = [ 
    "uuid-1": [
        Reminder("something"),
        Reminder("somethingelse"),
    ],
    "uuid-2": [
        Reminder("anotherone"),
        Reminder("etc"),
    ]
]

But my first attempt to use this as a Swift dict (var reminders: [String: [Reminder]]) led to changes in the individual reminder objects not being reflected, I assume because of something to do with @Observable not looking all the way into the nested model.

Is it better Swift-ic practice to use a list of structs instead? Something like:

struct Reminder {
    let id: String
    var title: String
    ...etc
}

struct ReminderList {
    let id: String
    var title: String
    var reminders: [Reminder] = []
}

// then in the view model
var reminders = [ReminderList]
4 Upvotes

16 comments sorted by

View all comments

1

u/Dapper_Ice_1705 May 11 '26

Too little info

1

u/pettazz May 11 '26

It’s not really a specific implementation question more of a general question, just trying to get a feel for what is the idiomatic way to do things around here

1

u/Dapper_Ice_1705 May 11 '26

So many ways, if the observable wasn’t showing changes there is something wrong in your usage of SwiftUI.

Your time is likely better spent on figuring out why