简体   繁体   中英

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? 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. Can anyone help me solve this? Thanks for the help. Also, I have inputted LocationViewController as my Storyboard ID with Use storyboard id unchecked. (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().

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

1) Find a way to fix your locationService.status

2) Bypass the switch statement

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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