【发布时间】:2020-11-24 17:05:03
【问题描述】:
我正在尝试在 SwiftUI 视图初始化程序中引用环境对象来设置状态值,但我收到了错误 'self' used before all stored properties are initialized。有没有办法做到这一点,因为您需要引用 self 来访问环境对象?我觉得引用继承的值是您应该能够在视图构造中执行的操作。
struct Example: View {
@EnvironmentObject var object: Items
@State var ints: Array<Int>
init() {
self._ints = State(initialValue: Array(repeating: 0, count: self.object.items.count))
}
}
【问题讨论】:
标签: constructor swiftui state combine environmentobject