无限循环和线程 1:EXC_BAD_ACCESS(代码=2,地址=0x7ffeedaec488)

Infinite Loop and Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeedaec488)

我在 tabBar.viewControllers = [cameraVC, galleryVC] 的数组中得到线程 1: EXC_BAD_ACCESS (code=2, address=0x7ffeedaec488) 在我的控制台中我得到我的用户名没完没了地打印出来,它来自不同的 class。第一个代码片段显示了线程的显示位置:

func addTabBar() {
    let cameraVC = HomeViewController()
    let galleryVC = GalleryViewController()

    let tabBarItem1 = UITabBarItem(title: "Camera", image: UIImage(named: "camera-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "blueCamera-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
    let tabBarItem2 = UITabBarItem(title: "Gallery", image: UIImage(named: "gallery-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "blueGallery-icon")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))



    cameraVC.tabBarItem = tabBarItem1
    galleryVC.tabBarItem = tabBarItem2
    tabBarItem2.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -6, right: 0)

    tabBar.viewControllers = [cameraVC, galleryVC]

    self.view.addSubview(tabBar.view)
}

这是我的第二个代码片段,其中定义了我在控制台中不断重复的用户名打印语句的所有函数:

@IBAction func signUpTapped(_ sender: Any) {
    //validate the fields
    let error = validateFields()
    
    if error != nil {
        //something's wrong, show error message
        showError(error!)
    }
    else {
        
        //create trimmed data
        let userName = userNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        //create the user
        Auth.auth().createUser(withEmail: email, password: password) { (result,err) in
            
            //check for errors
            if err != nil {
                //error creating user
                self.showError(err?.localizedDescription ?? "Error creating user")
            }
            else {
                    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
                    changeRequest?.displayName = userName
                    print(userName)
                changeRequest?.commitChanges { (error) in print ("Username not created") }

                //transition to home screen
                self.saveLoggedState()
                self.transitionHome()
            }
        }
    }
}
func showError(_ message:String){
    
    errorLabel.text = message
    errorLabel.alpha = 1
}
func saveLoggedState() {
    
    let def = UserDefaults.standard
    def.set(true, forKey: "is_authenticated")
    def.synchronize()
    
}
func transitionHome() {
    
    let homeVC = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
    self.view.window?.rootViewController = UINavigationController(rootViewController: homeVC!)
    self.view.window?.makeKeyAndVisible()
    
}

我希望这些代码足够了!我真的很困惑问题是什么,之前的堆栈溢出帖子没有帮助。任何帮助将不胜感激。谢谢!

编辑 1:我还在家庭控制器中实现了侧边菜单。它的代码如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundColor = color
    view.backgroundColor = color
    tableView.tableFooterView = UIView()
}

// Table
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = menuItems[indexPath.row].rawValue
    cell.textLabel?.textColor = .white
    cell.backgroundColor = color
    cell.contentView.backgroundColor = color
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    // Relay to delegate about menu item selection
    let selectedItem = menuItems[indexPath.row]
    delegate?.didSelectMenuItem(named: selectedItem)
}
func addTabBar() {
   let cameraVC = HomeViewController()
   let galleryVC = GalleryViewController()
}

我猜这是一个错字,你在 HomeViewController 中创建了 HomeViewController,这会导致无限循环,我认为你的 Auth 是在内部调用的 viewViewAppear/didLoad,所以它会无休止地触发,直到你 Stack overflow。

如果不是这种情况,我猜你的 HomeViewController 会触发 signUpTapped,然后在用户创建时它会返回到 HomeViewController(再次触发 signUpTapped),所以递归没有结束。