Member-only story

SwiftUI: StateObject vs ObservableObject

How new StateObject is different than ObservableObject?

Prafulla Singh
1 min readJun 23, 2020

Apple introduced a new property wrapper for iOS-14 named StateObject. This new property wrapper suppose to use if the object is being created inside of view Struct.
SwiftUI’s Views consider that an ObservableObject will always be referenced by some outside class, As SwiftUI’s view are struct. An ObservableObject has possibility to getting deallocate and reinitialized.

To support preservation of object initialized within view. Apple introduced this new Property wrapper called StateObject.

Usage:

If Init ObservableObject type class in the view struct, use Property StateObject otherwise use ObservableObject.

--

--

Prafulla Singh
Prafulla Singh

No responses yet