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

Ios 在Swift 4中加载应用程序时发生崩溃?,ios,swift,Ios,Swift,当应用程序加载时出现以下错误:未能实例化UIMainstryBoardFile“Main”的默认视图控制器-可能未设置指定的入口点?因此,在Main.Storyboard中,我没有检查是否为初始视图控制器,因为正如您在我的代码中看到的,我正在我的AppDelegate中执行此操作,但当应用程序运行时,它会崩溃并在我的assertionFailure()catch上停止。谁能帮我解决这个问题?谢谢你的帮助。此外,我还输入了LocationViewController作为我的故事板ID,未选中使用故

当应用程序加载时出现以下错误:
未能实例化UIMainstryBoardFile“Main”的默认视图控制器-可能未设置指定的入口点?
因此,在
Main.Storyboard
中,我没有检查
是否为初始视图控制器
,因为正如您在我的代码中看到的,我正在我的
AppDelegate
中执行此操作,但当应用程序运行时,它会崩溃并在我的
assertionFailure()
catch上停止。谁能帮我解决这个问题?谢谢你的帮助。此外,我还输入了
LocationViewController
作为我的故事板ID,未选中
使用故事板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
}
}
class AppDelegate:UIResponder、UIApplicationLegate{
let window=UIWindow()
设locationService=locationService()
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
let service=MoyaProvider()
让jsonDecoder=jsonDecoder()
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意]?)->Bool{
jsonDecoder.keyDecodingStrategy=.convertFromSnakeCase
服务请求(.search(lat:34.148000,long:-118.361443)){(result)in
切换结果{
成功案例(让我们回答):
让root=try?self.jsonDecoder.decode(root.self,from:response.data)
打印(根)
案例。失败(let错误):
打印(“错误:\(错误)”)
}
}
让locationViewController=storyboard.InstanceEviewController(标识符为“locationViewController”)作为locationViewController
locationViewController?.locationService=locationService
window.rootViewController=位置ViewController
window.makeKeyAndVisible()的
返回真值
}
}

您的应用程序正在崩溃,因为您的locationService.status处于默认状态,因此它总是到达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
}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意])->Bool{
jsonDecoder.keyDecodingStrategy=.convertFromSnakeCase
服务请求(.search(lat:34.148000,long:-118.361443)){(result)in
切换结果{
成功案例(让我们回答):
让root=try?self.jsonDecoder.decode(root.self,from:response.data)

打印(根)等待,那么代码是进入.notDetermined案例还是默认案例?删除主界面字段中的“Main”,它应该可以正常工作,并检查此视频。它将帮助您在不使用情节提要的情况下开始编码。正确无误。它直接进入
.default
并调用
断言失败()
AssertionFailure将始终使应用程序崩溃。我认为您的locationService状态未正确启动。通常LocationServices在视图控制器中使用委托模式。您是否为此使用教程?我已将其从文本字段
Main
中删除,但它仍会在
AssertionFailure()上崩溃
因此,我不需要第二条switch语句。但是,当我应用你的代码时,它会运行我的应用程序,但在控制台上显示
nil
如果你想实际使用LocationService.status(你没有正确使用),你只需要第二条switch语句请参阅更新的答案为什么您会收到nil请参阅更新的问题,我以与您相同的方式实现并显示
nil
.mhmm