|
I took a sample from Paul's samples to simply display a list of game scores. I understand that a struct is "real data" and a class is a reference to data. I changed the definition of the SimpleGameResult from a struct to a class. In the example below, a score is changed in the .onAppear. The new value is not displayed unless something else on the view is changed. The myCounter += 1 is commented out in this code, and the .onAppear score = 99 does not show in the view. Uncomment the myCounter += 1 and the 99 shows correctly since the view has changed. if the SimpleGameResult is a struct, the .onAppear score of 99 appears correctly without the nei'cessity if changing something else in the view. o.k. so the results isn't really changing since the score in the class is changed, but the value that should be displayed has changed, so why would the view not consider itself changed? I'm doing this in a macOS app thus the .frame for sizing it's the concept that is escaping me and I really need an explanation please.
|
|
Because
This is why reference types need to be This is also why Your particular case is a bit more complicated since the The reason why you will see the changes if you also update Make sense? |
|
Thanks @roosterboy, that is exactly what I thought was happening and I did not want to accept my understanding as a fact. Now the problem is how do you make an array an observedobject? I had forgotten about the observableobject since I have not used it lately, but, how do you make an array of observableobjects. I changed the previous example to the following, but I am getting the same results of not updating until something is changed on the view, ie myCounter += 1 refreshes the view, the .onappear does not. I did read in passing one solution that @observedobject does not work on an array. Maybe that is this latest problem?
|
|
The easiest way to do it is to extract the code within the
(I would also change But I would first ask if |
|
@roosterboy interesting reading and I have a supplementary question about a class being a reference. Imagine if I have a swift file defining a class which has variables and methods. This is instantiated in ContentView. If I also have another view and declare a variable of the class when I navigate to that view is it acceptable to pass a reference to the view to give the functionality of the class to the new view Example below and sorry if this is in the wrong thread.
|
|
@roosterboy ... that is the old "keep you views small and simple" trick. Thanks. When you only have one thing that changes, rather than an array of things that change, the world(view) is a happy thing. This problem popped up while I was putting two classes together with inheritance. Structs work just fine if life was so simple. @Quindownunder ... you got the concept. That is exactly what is going on here. Sure saves a lot of coding when the class functionality is just passed on. Your can do the same with a struct, they are so interchangable if you do not need one of the class features. |
|
@Quindownunder, you could do that but you'd have to watch out for your class object being deallocated out from under you. This danger was one of the reasons why @rbquick:
That's one of the great things about Swift; you can usually use structs and protocols to handle what is done with inheritance in OO languages. Really, there are far fewer places in Swift and SwiftUI where a class is needed. Working with Apple's built-in objects is the main one, e.g. subclassing |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.