1

I'm Use structures by default when possible, what's the best practice to update the original struct instance a copy of it was passed to another variable. let's say I have a Post struct

struct Post {
    let title: String
    let likes: Int
    let viewsCount:Int
    var comments:[Comment]
}

and we have a simple Master-Details Screens when pushing the details screen we are copying the post to the details scene, and there the data may be changed as likes increments, added comments and so on, so when pop This ViewController, to the new data model to update the original model in the Master ViewController. what are the possible solution and the best practices for it? assuming we are using MVC or MVP

Bassem Tourky
  • 482
  • 5
  • 12
  • 1
    Pass the modified struct back with one of the methods described [here](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers). Otherwise, use a class. – Sweeper Feb 21 '20 at 15:23
  • I asked pretty much this exact quesiton, but it got a -1 and got deleted lol https://stackoverflow.com/q/58809724/3141234 – Alexander Feb 21 '20 at 15:28
  • @Alexander-ReinstateMonica Argh. The deletion appears to have been automatic (because the question was downvoted and then sat idle). Let's see if we can't remedy that. – matt Feb 21 '20 at 16:31
  • @matt It's a shame it got down voted. It's a bit wordy, but I think it's a pretty clear and well reasoned question ‍♂️ – Alexander Feb 21 '20 at 16:54

4 Answers4

1

You could create a common shared model class that is responsible for holding the Post data and that both view controllers could use and observe.

Ralf Ebert
  • 3,556
  • 3
  • 29
  • 43
1

This really has nothing to do with structs; it is simply the usual issue of passing data into a view controller on creation and passing data back from that view controller on destruction. It happens that in your case you are positing that this is the "same" data — i.e. you pass a Post to the detail view controller and you pass a Post back from the detail view controller — but that is just a contingent fact.

So, to answer the question as formulated, you would use any of the usual techniques. The detail view controller would need to pass the modified Post back to the master view controller. It could use a Notification or (more directly) you could use the standard protocol-and-delegate architecture.


On the other hand, if Post is the basis of your app's data, it would not be unreasonable to argue that the premise itself is flawed: this should have been a class all along, not a struct, exactly so that the data can be maintained in a central place and references to it can be maintained in different places.

Indeed, if things are more complex, you might have the app's data (including the Post) live in some third location off in model-data-space, and have all view controllers send a notification up to the data when they change it and have the data send a notification down to all view controllers in response (that is what Ralf Ebert's answer quite reasonably suggests). That sort of thing is a lot easier nowadays because (in iOS 13) we have observable objects and the Combine framework.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I would create helper functions and make them mutating. e.g.

mutating func incrementLikes() { 
   likes += 1 
}

Make sure your properties are var.

koen
  • 5,383
  • 7
  • 50
  • 89
Loren Rogers
  • 325
  • 3
  • 13
0

Ideally classes are best for the role you described as object maintain identity. Each post has an identity. According to apple docs "use classes when you need to control the identity of the data you're modeling" When you share a class instance across your app, changes you make to that instance are visible to every part of your code that holds a reference to that instance. Use classes when you need your instances to have this kind of identity. Classes vs strut

Abhiraj Kumar
  • 160
  • 1
  • 6