Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swiftui中从我的应用程序中的任何视图访问:@Environment(\.managedObjectContext)var上下文?_Ios_Swift_Core Data_Swiftui - Fatal编程技术网

Ios 如何在swiftui中从我的应用程序中的任何视图访问:@Environment(\.managedObjectContext)var上下文?

Ios 如何在swiftui中从我的应用程序中的任何视图访问:@Environment(\.managedObjectContext)var上下文?,ios,swift,core-data,swiftui,Ios,Swift,Core Data,Swiftui,我只能在ContentView()中访问和使用此“@Environment(.managedObjectContext)var context”,如果我尝试在任何其他视图中访问“@Environment(.managedObjectContext)var context”,我将收到以下错误: “环境中的上下文未连接到永久存储协调器:” 如何从我的应用程序中的任何视图访问它?您需要在scenedelegate.swift中找到它 // Create the SwiftUI view and se

我只能在ContentView()中访问和使用此“@Environment(.managedObjectContext)var context”,如果我尝试在任何其他视图中访问“@Environment(.managedObjectContext)var context”,我将收到以下错误: “环境中的上下文未连接到永久存储协调器:”


如何从我的应用程序中的任何视图访问它?

您需要在scenedelegate.swift中找到它

 // Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
    // Add `@Environment(\.managedObjectContext)` in the views that will need the context.
    let contentView = ContentView().environment(\.managedObjectContext, context)
从各个角度看

  @Environment(\.managedObjectContext) var context
然后可以访问每个视图中的上下文


或者查看此链接

我已经完全做到了,并且我只能在子视图中访问上下文,例如,如果我在ContentView中有一个SomeView(),那么我就可以访问它。但是如果我试图在ContentView()之外的某个视图中访问它,我将无法访问它