简体   繁体   English

在 Swift 4 中加载应用程序时崩溃?

[英]Getting crash when application is loading in Swift 4?

I am getting a crash when app loads up with this error: Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?当应用程序加载时出现此错误,我崩溃了: Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set? So, in Main.Storyboard I don't check is initial view controller because as you can see in my code I am doing it in my AppDelegate , but when app runs it crashes and stops on my assertionFailure() catch.因此,在Main.Storyboard我不检查is initial view controller因为正如您在我的代码中看到的那样,我在AppDelegate执行此操作,但是当应用程序运行时,它会崩溃并在我的assertionFailure()捕获时停止。 Can anyone help me solve this?谁能帮我解决这个问题? Thanks for the help.谢谢您的帮助。 Also, I have inputted LocationViewController as my Storyboard ID with Use storyboard id unchecked.此外,我已输入LocationViewController作为我的 Storyboard ID, Use storyboard id选中Use storyboard id (I even checked it and still same error). (我什至检查过它仍然是同样的错误)。

Here is my code:这是我的代码:

class AppDelegate: UIResponder, UIApplicationDelegate {
    let window = UIWindow()
    let locationService = LocationService()
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let service = MoyaProvider<YelpService.BusinessesProvider>()
    let jsonDecoder = JSONDecoder()

       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
    service.request(.search(lat: 34.148000, long: -118.361443)) { (result) in
        switch result {
        case .success(let response):
            let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
            print(root)
        case .failure(let error):
            print("Error: \(error)")
        }
    }

    let locationViewController = storyboard.instantiateViewController(withIdentifier: "LocationViewController") as? LocationViewController
    locationViewController?.locationService = locationService
    window.rootViewController = locationViewController

    window.makeKeyAndVisible()

    return true
}
}

Your app is crashing because your locationService.status is defaulting so it is always reaching assertionFailure().您的应用程序崩溃是因为您的 locationService.status 处于默认状态,因此它总是达到 assertionFailure()。

Use this function to stop the program, without impacting the performance of shipping code, when control flow is not expected to reach the call—for example, in the default case of a switch where you have knowledge that one of the other cases must be satisfied https://developer.apple.com/documentation/swift/1539616-assertionfailure当控制流预计不会到达调用时,使用此函数停止程序,而不会影响交付代码的性能 - 例如,在交换机的默认情况下,您知道必须满足其他情况之一https://developer.apple.com/documentation/swift/1539616-assertionfailure

1) Find a way to fix your locationService.status 1) 找到一种方法来修复您的 locationService.status

2) Bypass the switch statement 2)绕过switch语句

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
    service.request(.search(lat: 34.148000, long: -118.361443)) { (result) in
        switch result {
        case .success(let response):
            let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
            print(root) <-- Console is printing nil here because your jsonDecoder failed.
        case .failure(let error):
            print("Error: \(error)")
        }
    }

    let locationViewController = storyboard.instantiateViewController(withIdentifier: "LocationViewController") as? LocationViewController
    locationViewController?.locationService = locationService
    window.rootViewController = locationViewController

    window.makeKeyAndVisible()

    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM