gpt4 book ai didi

swiftui - 用另一个 StateObject 初始化 StateObject

转载 作者:行者123 更新时间:2023-12-04 08:46:25 25 4
gpt4 key购买 nike

我有这个 App 结构,我需要用 userManager 初始化 appearancesStore 和 behavioursStore。但是我收到错误 Escaping auto closure captures mutating 'self' parameterVariable 'self.appearancesStore' captured by a closure before being initialized

我尝试了一些不同的方法来做到这一点,但都失败了。有没有人建议如何执行此操作?

@main
struct DuneApp: App {
@StateObject var userManager = UserManager()
@StateObject var appearancesStore: AppearancesStore
@StateObject var behavioursStore: BehavioursStore

init() {
_appearancesStore = StateObject(wrappedValue: AppearancesStore(manager: self.userManager))
_behavioursStore = StateObject(wrappedValue: BehavioursStore(manager: self.userManager))
}

var body: some Scene {
WindowGroup {
AuthenticateView()
.environmentObject(userManager)
.environmentObject(appearancesStore)
.environmentObject(behavioursStore)
}
}
}

最佳答案

尝试以下操作

@main
struct DuneApp: App {
@StateObject var userManager: UserManager
@StateObject var appearancesStore: AppearancesStore
@StateObject var behavioursStore: BehavioursStore

init() {
let userManager = UserManager()

_userManager = StateObject(wrappedValue: userManager)
_appearancesStore = StateObject(wrappedValue: AppearancesStore(manager: userManager))
_behavioursStore = StateObject(wrappedValue: BehavioursStore(manager: userManager))
}

var body: some Scene {
WindowGroup {
AuthenticateView()
.environmentObject(userManager)
.environmentObject(appearancesStore)
.environmentObject(behavioursStore)
}
}
}

关于swiftui - 用另一个 StateObject 初始化 StateObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64291745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com