gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

转载 作者:行者123 更新时间:2023-11-29 01:20:03 25 4
gpt4 key购买 nike

您好,我在尝试为每个屏幕尺寸分配不同的 sotryboard 时遇到错误:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

如何解决这个问题?

我还得到:

EXC_BAD_INSTRUCTION ERRROR

我可以在 Objective-C 中做到这一点,但不能在 Swift 中工作。

此外,如果有更好的方法,请告诉我。

谢谢。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
func grabStoryboard() -> UIStoryboard {
var storyboard = UIStoryboard()
let height = UIScreen.mainScreen().bounds.size.height

if height == 480 {
storyboard = UIStoryboard(name: "main3.5", bundle: nil)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

let storyboard: UIStoryboard = self.grabStoryboard()

self.window?.rootViewController =
storyboard.instantiateInitialViewController()! as UIViewController
self.window?.makeKeyAndVisible()
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

最佳答案

您正在调用返回可选 UIViewController 的 instantiateInitialViewController。如果初始 View Controller 未在 Storyboard上设置,则此调用将失败,因为结果为 nil。所以你应该检查两个 Storyboard都设置了初始 View Controller 。你可以做这样的事情来防止它失败:

if let initialVC = storyboard.instantiateInitialViewController() {
self.window?.rootViewController = initialVC
self.window?.makeKeyAndVisible()
}

此外,在您的grabStoryboard函数中,您正在实例化一个 Storyboard:varstoryboard = UIStoryboard(),但然后立即用其他东西覆盖它。您可以将其更改为:

func grabStoryboard() -> UIStoryboard {
let storyboard: UIStoryboard
let height = UIScreen.mainScreen().bounds.size.height

if height == 480 {
storyboard = UIStoryboard(name: "main3.5", bundle: nil)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}

您正在根据高度加载不同的 Storyboard ,这可能会在新设备出现不同尺寸时导致问题。您可以使用一个带有尺寸等级的 Storyboard。 Read about it here

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34719442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com