Ios 原因:'-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:发送到实例0x282efc980'的无法识别的选择器;
当我试图运行项目时,出现以下错误,我的项目正在崩溃 它给出了以下错误: FaveoHelpdeskPro_Swift[1400:370341] -[FaveOHelpedeskPro_Swift.AppDelegate窗口]:发送到实例0x282efc980的无法识别的选择器 及 ***由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因: '-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:无法识别的选择器 已发送到实例0x282efc980' 接下来的屏幕上 我的Ios 原因:'-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:发送到实例0x282efc980'的无法识别的选择器;,ios,swift,ios13,Ios,Swift,Ios13,当我试图运行项目时,出现以下错误,我的项目正在崩溃 它给出了以下错误: FaveoHelpdeskPro_Swift[1400:370341] -[FaveOHelpedeskPro_Swift.AppDelegate窗口]:发送到实例0x282efc980的无法识别的选择器 及 ***由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因: '-[FaveoHelpdeskPro_Swift.AppDelegate窗口]:无法识别的选择器 已发送到实例0x2
SceneDelegate.swift
已经包含窗口对象
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
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).
guard let _ = (scene as? UIWindowScene) else { return }
}
问题是什么,为什么会崩溃
您的
AppDelegate
不包含窗口
引用。在AppDelegate
var window: UIWindow?
摘自苹果的
此属性包含用于在设备主屏幕上显示应用程序可视内容的窗口
如果应用程序的Info.plist文件包含UIMainstryBoardFile密钥,则需要实现此属性
您的
AppDelegate
不包含窗口
引用。在AppDelegate
var window: UIWindow?
摘自苹果的
此属性包含用于在设备主屏幕上显示应用程序可视内容的窗口
如果应用程序的Info.plist文件包含UIMainstryBoardFile密钥,则需要实现此属性
当您开始在项目中的SceneDelegate中初始化主ViewController时,一些UI库(如SVProgressHUD,…)仍然指向AppDelegate的
窗口,但没有任何内容,因此它会崩溃。我已通过以下方式遇到并成功解决了此问题:
在AppDelegate
中声明一个UIWindow,如果它不在那里,还声明一个静态变量以快速访问AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // HERE, add it if it's not available.
static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
//...
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? // HERE
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = YOUR_VIEW_CONTROLLER()
window?.makeKeyAndVisible()
AppDelegate.shared.window = window // Connect it HERE!
}
// ....
}
在SceneDelegate
中,将UIWindow对象引用回AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // HERE, add it if it's not available.
static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
//...
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? // HERE
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = YOUR_VIEW_CONTROLLER()
window?.makeKeyAndVisible()
AppDelegate.shared.window = window // Connect it HERE!
}
// ....
}
注意:如果您刚刚完成步骤1,它将不会崩溃,但某些UI组件将显示错误,因为它们无法确定哪个是主窗口。当您开始在项目中的SceneDelegate和一些UI库(如SVProgressHUD,…)中初始化主ViewController时仍然指向AppDelegate的窗口
,但没有任何内容,因此它会崩溃。我已通过以下方式遇到并成功解决了此问题:
在AppDelegate
中声明一个UIWindow,如果它不在那里,还声明一个静态变量以快速访问AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // HERE, add it if it's not available.
static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
//...
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? // HERE
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = YOUR_VIEW_CONTROLLER()
window?.makeKeyAndVisible()
AppDelegate.shared.window = window // Connect it HERE!
}
// ....
}
在SceneDelegate
中,将UIWindow对象引用回AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // HERE, add it if it's not available.
static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
//...
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? // HERE
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = YOUR_VIEW_CONTROLLER()
window?.makeKeyAndVisible()
AppDelegate.shared.window = window // Connect it HERE!
}
// ....
}
注意:如果您刚刚完成步骤1,它将不会崩溃,但某些UI组件将显示错误,因为它们无法确定哪个是主窗口。因此,我刚才所做的是将UIWindow变量添加到我的AppDelegate中,如下所示:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
...
然后在SceneDelegate中,当我设置UIWindow属性时:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.rootViewController = MainTabBarController()
window?.windowScene = windowScene
window?.makeKeyAndVisible()
我还为AppDelegate设置了它,如下所示:
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.window = window
}
我刚才做的是在AppDelegate中添加了一个UIWindow变量,如下所示:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
...
然后在SceneDelegate中,当我设置UIWindow属性时:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.rootViewController = MainTabBarController()
window?.windowScene = windowScene
window?.makeKeyAndVisible()
我还为AppDelegate设置了它,如下所示:
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.window = window
}
在AppDelegate中添加“var窗口:UIWindow?”并重试。将该窗口对象放入AppDelegate类中,因为某个类正试图从appdel访问该窗口对象。太好了,它现在正在工作,您知道它为什么会崩溃吗?添加“var窗口:UIWindow?”在您的AppDelegate中,并尝试。将该窗口对象放入AppDelegate类中,因为某个类正试图从appdel访问该窗口对象。太好了,它现在正在工作,您知道它为什么会崩溃吗?