NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

SOLVED: How to initialize @StateObject in initializer

Forums > SwiftUI

Having watched Paul's latest birthday video, I've vowed to use @StateObject properly, which of course lands me in trouble almost immediately:

struct ArkansasApp: App {
    @StateObject var appState = AppState()
    @StateObject var arkansasScene: ArkansasScene

    init() {
    // Problem here: compiler says "Escaping autoclosure captures mutating 'self' parameter"
    // I'm only barely grasping what the message means, so naturally I have no idea what to
    // do to fix it.
        _arkansasScene = StateObject(wrappedValue: ArkansasScene(appState: _appState))

    var body: some Scene {
        WindowGroup {

I was doing this with @ObservedObject before, and it was working fine, but I'm kind of fussy about doing things the right way, plus Paul says it might not always behave properly at runtime. Anyone have any advice?


Try to initialize the appState also in your initializer.


_appState refers to the @StateObject property wrapper. Maybe you mean appState, which refers to the wrapped value? I don't know what the initializer for ArkansasScene looks like.


Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.