@StateObject
The @StateObject Wrapper didn’t appear like the others in iOS 13, but only with iOS 14. The @StateObject and @ObservedObject Wrapper have lots in common, such as both being able to use the ObservableObject protocol to observe a class. Nevertheless, they differ in one important point.
The @StateObject wrapper keeps the object, even if the view is reloaded. This is different than with @ObservedObject, where after each reload of the view, the object is reloaded as well. So you could say that the @StateObject wrapper is a combination of @State and @ObservedObject. In addition, the wrapper should only be used once per object, namely in the view that is responsible for creating the object. All other views that then want to access our object, for example when passing it down to another view, should use @ObservedObject.
An example that demonstrates this could look like this:

In this example, we have a ContentView and a ListCars View that gives us a list of car brands. The ListView consists of our @StateObject property and a button where we can add a new car brand to the array from our object. If the button is pressed, the view is reloaded and the new car brand appears in the list. In order to show that @StateObject retains the object and our values, a @State Property was created in the ContentView. Once the button is pressed by incrementing the counter, the view is also reloaded. This way we can see that our added car brand still appears in the list because @SateObject retains our object and its values when the view is reloaded.
Let’s now swap our @StateObject Wrapper with the @ObservedObject Wrapper and run the whole example again from the beginning, we see that after pressing the Counter-Button, our car brand disappears from the list. This is because @ObservedObject does not retain the object and its values once the View is reloaded.
@StateObject
Der @StateObject Wrapper erschien nicht wie die anderen in iOS 13, sondern erst mit iOS 14. Der @StateObject und @ObservedObject Wrapper haben vieles gemeinsam, wie zum Beispiel, dass beide das Protocol ObservableObject verwenden können, um eine Klasse zu beobachten. Dennoch unterscheiden sie sich in einem wichtigen Punkt.
Der @StateObject Wrapper behält das Objekt, auch wenn die View neu geladen wurde. Das ist anders als beim @ObservedObjekt, wo nach jedem neu laden der View das Objekt mit neu geladen wird. Man kann also sagen, dass der @StateObject Wrapper eine Kombination aus @State und @ObservedObject ist. Zudem sollte der Wrapper auch nur einmal pro Objekt verwendet werden, und zwar in der View, die für die Erstellung des Objektes verantwortlich ist. Alle anderen Views, die danach Zugriff auf unser Objekt haben wollen, wie zum Beispiel beim Heruntergeben zu einer anderen View, sollten @ObservedObject benutzen.
Ein Beispiel das, dass ganze veranschaulichen soll, könnte wie folgt aussehen:

In diesem Beispiel haben wir eine ContentView und eine ListCars View, die uns eine Liste von Automarken ausgibt. Die ListView besteht aus unserer @StateObject Property und einem Button, wo wir eine neue Automarke dem Array aus unserem Objekt hinzufügen können. Wird der Button betätigt, so wird die View neu geladen und die neue Automarke erscheint in der Liste. Um nun zu zeigen, dass @StateObjekt das Objekt und unsere Werte behält, wurde in der ContentView eine @State Propertys erstellt. Sobald der Button betätigt wird, indem der Counter hochgezählt wird, wird auch die View neu geladen. So können wir sehen, dass unsere hinzugefügte Automarke immer noch in der Liste auftaucht, da @SateObject unser Objekt und deren Werte behält, wenn die View neu geladen wird.
Tauschen wir jetzt unseren @StateObject Wrapper mit dem @ObservedObject Wrapper aus und führen das ganze Beispiel nochmal von vorne durch, sehen wir, dass nach Betätigen des Counter-Buttons, unsere Automarke aus der Liste verschwindet. Dies liegt da dran, dass @ObservedObject das Objekt und deren Werte nicht behält, sobald die View neu geladen wird.
