我正在研究MyLocations应用程序的保存位置章节,并在当我添加代码设置选项卡栏控制器作为App委托中的root时,将错误发现错误,为什么会发生这种情况,我如何解决这个问题?

任何人?真的需要帮助

你好 @ isaacballas2.,看起来表明返回零并崩溃,因为您正在使用强制展开。一种检查此方法的方法是使用保护语句。关于解决方案,我会尝试将TabController更改为常量,
让TabController = mainstoryboard.instantiateViewControllerWith identifier(“您的Storyboard”)
作为! UitabbarController.
自己.window?.rootviewController = tabcontroller

最好,
吉娜

它甚至不会编译,它表示主机库是未知的标识符。我跟随IOS学徒的方向,这本书有一个错误吗?

@fahim any thoughts?

这是完整的错误
致命错误:展开可选值时出乎意料地发现nil:文件/用户/ isaacballas / library / cloudstorage / iCloud驱动器/桌面/ locationtracker / locationtracker / appdelegate.swift,第30行

如果没有看到你的项目,我无法肯定地告诉你,但它看起来好像你的根视图控制器可能不是选项卡视图控制器。请将您的项目与本章(特别是故事板)的最终项目进行比较,并查看两者是否以相同的方式设置。希望这让你弄清楚发生了什么......

当我设置项目时,我将其设置为单个页面项目,然后在故事板上实现了标签栏......是我的错误吗?我可以解决它吗?如何使Tab栏控制器root制作?

我的建议是要检查故事板,并确保箭头指示rootvc(初始VC.)连接到标签栏VC。您可能没有声明的初始VC或指向非选项卡栏VC。因此,崩溃是因为没有rootviewController,或者它不能拍摄rootViewController并将其投用为UITabBarController。

我刚检查过,它连接到Tab栏,这是一个屏幕截图,我没有为什么会发生这种情况

另外两个建议。

  1. 尝试执行此应用程序的干净:按住选项,转到“清洁”构建文件夹。然后,转到Xcode首选项并清除派生数据。 ( //stackoverflow.com/questions/46468220/how-can-i-delete-derived-data-in-xcode-9/46468462)

  2. 替换有三行的代码!并将其转换为三行代码。这将至少允许您弄清楚导致Xcode崩溃的东西。

消除:
tabbarcontroller = window!.rootViewController 作为 ! UITabBarController

代替:
窗口= 自己。窗户!
rootvc = window.rootViewController!
tabbarcontroller = rootvc. 作为 ! UITabBarController

对上述问题的任何解决方案?

@Filly. 你还有这个问题吗?

我也遇到了这个问题。这个问题有什么解决方案吗?我的情况完全相同 @ isaacballas2..

我可能已经在Matt Neuburg的书“编程iOS 13”的书中找到了解决方案 安德烈班克罗夫特的页面. He writes that as of iOS 13 the app is launched with window scene support, which alters the order of assignments and, most importantly, the call to (_:didFinishLaunchingWithOptions:). I’ll quote here:

“Exactly how UIApplicationMain proceeds depends on what it discovers as it gets going. New in iOS 13, your app can use scenes and scene-related classes and protocols (UISceneSession, UIScene, UIWindowScene, UIWindowSceneDelegate).”

据我所知,提交人和他在发布过程中概述的步骤,在iOS 12和旧的推出中用于继续如下:

  1. UIApplicationMain looks to see whether your app uses a main storyboard; if so, it instantiates the storyboard’s first view controller (that would be your UITabBarController I presume).

  2. UIApplicationMain assigns the window (of type UIWindow) to the app delegate’s window property.

  3. 然后,它继续为窗口的根视图控制器(这是您有问题的代码行是指的)来分配步骤1中实时的视图控制器(这是一个),它将其保留在内存中。

  4. 它呼叫 (_:didFinishLaunchingWithOptions:)

This would be all well and good for your code, as the root view controller already exists when (_:didFinishLaunchingWithOptions:) is called. However, in iOS 13 the order changes to the following:

  1. After creating the application instance and assigning the app delegate class to its delegate property (I omitted that very first step above), UIApplicationMain immediately calls (_:didFinishLaunchingWithOptions:). This is problematic for your code because, at this point, not even a window exists yet and thus your force unwrap crashes the app.

  2. UIApplicationMain 继续创建Uiscenesession,UiWindowsCene和窗口场景委托的实例。

  3. 在此之后,它会检查您的应用程序是否使用故事板并初始化通常的启动过程(创建窗口,将第一个视图控制器的视图分配为根视图等)

  4. Lastly, it calls (_:willConnectTo:options:) in the SceneDelegate class.

Long story short, your code refers to something that does not exist at this point of time in the launching process. The fix would be to move your code over to the SceneDelegate's (_:willConnectTo:options:)method, which is called at the same point in time when (_:didFinishLaunchingWithOptions:) used to be called. Hope that helps.

2喜欢

@danielmey. 感谢您分享您的解决方案 - 非常感谢! :]

移动:listen forfatalcoredatanotifications()
来自:appdelegate:didfinishlaunchingwithoptions
to:scenedelegate:_:willconnectto:选项:

为我工作。

谢谢你的好写作。