如何在视图的init()中访问@EnvironmentObject

时间:2020-07-13 08:28:03

标签: swiftui

struct ContentView: View {
//    @ObservedObject var user = User()
    @EnvironmentObject var user: User
    
    init(){
        user.username = "yy"
    }

我想更改init()中的EvironmentObject值 因为我想要数据预处理

但是此代码有错误

Thread 1: Fatal error: No ObservableObject of type User found. A View.environmentObject(_:) for User may be missing as an ancestor of this view.

如何在View的初始化中访问EnvironmentObject?

0 个答案:

没有答案