Post

Replies

Boosts

Views

Created

Relationships between structs and detecting changes
I'm currently working on an app which has a number of different data objects with relationships between them. For example, there is a Building struct: struct Building { let id: UUID var name: String } There is also an Address struct, which has a Building property: struct Address: { let id: UUID var addressText: String var building: Building } When I change the name of the Building I would like it to update for all the corresponding Address objects. Because structs are value types, this doesn't happen at present. I could fix this by changing struct to class, but because I'm working with SwiftUI that's not desirable. I could fix this by updating the Address to store the id of the Building instead but having to look up the name of the corresponding building each time I want it feels messy to me. Is there some other approach I'm missing? Or a recommended best practice way of handling relationships between structs?
3
0
628
Jun ’22
Relationships between structs and detecting changes
I'm currently working on an app which has a number of different data objects with relationships between them. For example, there is a Building struct: struct Building { let id: UUID var name: String } There is also an Address struct, which has a Building property: struct Address: { let id: UUID var addressText: String var building: Building } When I change the name of the Building I would like it to update for all the corresponding Address objects. Because structs are value types, this doesn't happen at present. I could fix this by changing struct to class, but because I'm working with SwiftUI that's not desirable. I could fix this by updating the Address to store the id of the Building instead but having to look up the name of the corresponding building each time I want it feels messy to me. Is there some other approach I'm missing? Or a recommended best practice way of handling relationships between structs?
Replies
3
Boosts
0
Views
628
Activity
Jun ’22