You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@ObservedObject shound't be initialized inside the SwiftUI.View #6829
Please describe the rule idea, format this issue's title as Rule Request: [Rule Name] and describe:
New Rule Request:
Why should this rule be added?
Apple's documentation is explicit that a view must not create an @ObservedObject — @ObservedObject doesn't own the object's lifetime, so initializing one inside a View is unsafe: SwiftUI can recreate the view at any time, which re-instantiates the object (silently resetting its state) and, during teardown/animation, can leave SwiftUI's internal box referencing a deallocated object → EXC_BAD_ACCESS use-after-free crashes (ObservedObjectPropertyBox.update → objc_msgSend). The correct wrappers are @StateObject (view owns it) or @Observable (Observation). This rule flags objects created inside an @ObservedObject.
Apple: @ObserverdObject / @ObservedObject ("use @StateObject … to ensure consistent results" / don't create observed objects in a view) - "Don’t specify a default or initial value for the observed object. Use the attribute only for a property that acts as an input for a view, as in the above example."
Hacking with Swift - "Tip: It is really important that you use @ObservedObject only with views that were passed in from elsewhere. You should not use this property wrapper to create the initial instance of an observable object – that’s what @StateObject is for.
Examples
Triggers a violation (object created in the view):
@ObservedObject var viewModel = ViewModel()
@ObservedObject private var loader = Loader(url: url)
init() { self._state = ObservedObject(wrappedValue: LoadState()) }
Does not trigger (injected, not created):
@ObservedObject var viewModel: ViewModel // set from init parameter
init(viewModel: ViewModel) { self.viewModel = viewModel }
@StateObject var viewModel = ViewModel() // owned via @StateObject
@State var model = Model() // @Observable model
changed the title [-]@ObservedObject shound't be initialized inside the `SwiftUI.View`[/-][+]`@ObservedObject` shound't be initialized inside the `SwiftUI.View`[/+]on Jul 21, 2026
Activity
[-]@ObservedObject shound't be initialized inside the `SwiftUI.View`[/-][+]`@ObservedObject` shound't be initialized inside the `SwiftUI.View`[/+]