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

时间:2019-01-04 22:34:57

标签: ios swift

当应用加载此错误时,我崩溃了: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作为我的情节提要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
}
}

1 个答案:

答案 0 :(得分:1)

您的应用程序崩溃是因为您的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
}
相关问题