Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 当changeRootViewController时,出现未知错误:EXC\u错误\u访问(代码=1,地址=0x20)_Swift_Xcode - Fatal编程技术网

Swift 当changeRootViewController时,出现未知错误:EXC\u错误\u访问(代码=1,地址=0x20)

Swift 当changeRootViewController时,出现未知错误:EXC\u错误\u访问(代码=1,地址=0x20),swift,xcode,Swift,Xcode,我尝试使用swift代码更改rootViewController以注销,但似乎出现了内存错误: 注销代码如下: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let section = SettingsSection(rawValue: indexPath.section) else {return} switch section {

我尝试使用swift代码更改rootViewController以注销,但似乎出现了内存错误:

注销代码如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let section = SettingsSection(rawValue: indexPath.section) else {return}
    
    switch section {
    case .Profile:
        print(ProfileOptions(rawValue: indexPath.row)?.description)
        if ProfileOptions(rawValue: indexPath.row)?.description == "Log Out"{
            //logoutTapped()
            //return
            userDefaults.set("", forKey: Keys.username)
            userDefaults.set("", forKey: Keys.useremail)
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let loginNavController = storyboard.instantiateViewController(identifier: "LoginNavController")
            (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(loginNavController)
        }
        break
        
    case .Application:
        print(ApplicationOptions(rawValue: indexPath.row)?.description)
    }
}
SceneDelegate中的changeRootViewController代码:

func changeRootViewController(_ vc: UIViewController, animated: Bool = true) {
    guard let window = self.window else {
        return
    }
    window.rootViewController = vc
    // add animations
    UIView.transition(with: window,
                      duration: 0.5,
                      options: [.transitionCurlDown],
                      animations: nil,
                      completion: nil)
}
我遵循了本教程:
欢迎任何意见

要执行此操作,必须将当前的RootView存储在coreData中。首次使用CoreData打开应用程序时,必须预加载Base RootView

我也一直在做一个有可能发生这种变化的应用程序,最困难的事情是找到有关首次运行应用程序预加载数据的信息

下面是我使用的代码:

您必须在AppDelegate中预加载数据

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
           
//不要忘记设置该功能 预加载DataFromStructArray()

            return true
    }
    
    func preloadDataFromStructArray() {
        
        let preloadedDataKey = "didPreloadData"
        let userDefaults = UserDefaults.standard
        
        if userDefaults.bool(forKey: preloadedDataKey) == false {

            let someViewController1 = isCurrent(index: 0, status: false)
            let someViewController2 = isCurrent(index: 1, status: false)
            
            let backgroundContext = CoreDataStack.persistentContainer.newBackgroundContext()
            CoreDataStack.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
            
            backgroundContext.perform {
                
                    let arrayViewControllersStatuses = [someViewController1, someViewController2]
                    
                    do{
                    
                    for status in arrayViewControllersStatuses {

                        let viewControllerStatus = CurrentTrainingPlan(context: backgroundContext)
                        viewControllerStatus = status.status!
                        viewControllerStatus = status.index!
                        
                    }
                    
                        try backgroundContext.save()
                        userDefaults.setValue(true, forKey: preloadedDataKey)

                    } catch {
                        print(error.localizedDescription)
                    }
                
            }
                        
        }
        
    }