繁体   English   中英

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

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

当应用程序加载时出现此错误,我崩溃了: Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set? 因此,在Main.Storyboard我不检查is initial view controller因为正如您在我的代码中看到的那样,我在AppDelegate执行此操作,但是当应用程序运行时,它会崩溃并在我的assertionFailure()捕获时停止。 谁能帮我解决这个问题? 谢谢您的帮助。 此外,我已输入LocationViewController作为我的 Storyboard ID, Use storyboard id选中Use storyboard id (我什至检查过它仍然是同样的错误)。

这是我的代码:

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
}
}

您的应用程序崩溃是因为您的 locationService.status 处于默认状态,因此它总是达到 assertionFailure()。

当控制流预计不会到达调用时,使用此函数停止程序,而不会影响交付代码的性能 - 例如,在交换机的默认情况下,您知道必须满足其他情况之一https://developer.apple.com/documentation/swift/1539616-assertionfailure

1) 找到一种方法来修复您的 locationService.status

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