博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SwiftUI tutorials:Fatal error: No ObservableObject of type DataType found.
阅读量:3937 次
发布时间:2019-05-23

本文共 1586 字,大约阅读时间需要 5 分钟。

错误解决:

SwiftUI tutorials Fatal error: No ObservableObject of type UserData found.

错误记录:
在这里插入图片描述
解决方法:

  • 保证两个点:
  1. 添加.environmentObject(UserData())在两个地方:
  2. 第一个页面的 static var previews:some View{}中
  3. SceneDelagete.swift文件中,let contentView = xxx(你的第一个页面).environmentObject(UserData())

我解决的代码:

// LandmarkList.swiftstruct LandmarkList_Previews: PreviewProvider {
static var previews: some View {
LandmarkList() .previewDevice(PreviewDevice(rawValue: "iPhone 8")) .environmentObject(UserData()) } }//SceneDelegate.swift func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // Create the SwiftUI view that provides the window contents. // 这里是重点!!!! let contentView = LandmarkList().environmentObject(UserData()) // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() } }

转载地址:http://zjywi.baihongyu.com/

你可能感兴趣的文章
Linux下MySQL的安装
查看>>
linux下彻底卸载MySQL
查看>>
Linux 正则表达式
查看>>
Linux sed命令解析
查看>>
Linux awk命令详解
查看>>
Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
查看>>
IDEA中Struts2文件上传时404错误The origin server did not find a current representation for the target resour
查看>>
Perl/Tk 变量追踪及类线程实现
查看>>
1.嵌入式开发环境搭建--虚拟机安装(unbutu)系统
查看>>
2.嵌入式开发环境搭建--(unbutu)系统
查看>>
Linux USB驱动分析之USB2.0协议分析
查看>>
关于iwpriv :no private ioctls 的问题
查看>>
编写Java程序,做一个MPG和L/100KM的转换器
查看>>
c# winform ,在一個類中如何調用另外窗體的控件或方法
查看>>
c# winform BackgroundWorker 控件的使用
查看>>
SQL中 EXCEPT、INTERSECT用法
查看>>
SQL Server中VARCHAR(MAX)和NVARCHAR(MAX)的使用(转载)
查看>>
C# 对json字符串转换操作
查看>>
jQuery plugin: Validation 使用说明
查看>>
外部程序用process.start调用 其它exe文件时可以传入参数.
查看>>