0

下面的应用程序在执行源代码时崩溃: u.VerificaPermesso(...),应用程序检查 n 个调用节点的休息服务器。代码是正确的,但崩溃你能解释一下原因吗?调用 rest node.js 服务器,该服务器以 true 或 false 值响应,并取决于该值是否显示。

SWIFT代码:

import UIKit

class TabBarViewController: UITabBarController {

    var u: User = User()
    /* Genero L'UITabBarController creando le ViewController ed inserendole in un array! */
    override func viewDidLoad() {
        super.viewDidLoad()
        var tabFrame = self.tabBar.frame
        tabFrame.size.height = 60
        self.tabBar.frame = tabFrame
        hideKeyboardWhenTappedAround()



        let MarcaTempoView = MarcaTempoViewController()
        MarcaTempoView.tabBarItem = UITabBarItem(title: "Marca Tempo", image: UIImage(named: "clock.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 0)

        let CantieriView = CantieriViewController()
        CantieriView.tabBarItem = UITabBarItem(title: "Cantieri", image: UIImage(named: "home.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 1)

        let ArticoliView = RicercaArticoliViewController()
        ArticoliView.tabBarItem = UITabBarItem(title: "Articoli", image: UIImage(named: "articoli.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 2)

        let UserView = UserViewController()
        UserView.tabBarItem = UITabBarItem(title: "Utente", image: UIImage(named: "user.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 3)

        let ClienteView = ClienteViewController()
        ClienteView.tabBarItem = UITabBarItem(title: "Clienti", image: UIImage(named: "risorse_umane.png")?.scaleImage(toSize: CGSize(width: 10, height: 10)), tag: 4)



        var viewControllerList = [MarcaTempoView, CantieriView, ArticoliView, ClienteView,UserView]

        DispatchQueue.main.async {

            //Controllo permesso accesso cantieri
            let u = User()

            //Controllo permesso accesso marcatempo
            u.VerificaPermesso(TipologiaPermesso: "marcatempo", completion: { result in
                DispatchQueue.main.async {
                     if(result == "false") {
                    viewControllerList.removeAll(where:{ $0 is MarcaTempoViewController })

                    self.viewControllers = viewControllerList
                    }
                }
            });

            u.VerificaPermesso(TipologiaPermesso: "cantieri", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                       viewControllerList.removeAll(where:{ $0 is CantieriViewController })

                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Articoli
            u.VerificaPermesso(TipologiaPermesso: "articoli", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                         viewControllerList.removeAll(where:{ $0 is RicercaArticoliViewController })

                        self.viewControllers = viewControllerList
                    }
                }
            });

            //Controllo permesso accesso Clienti
            u.VerificaPermesso(TipologiaPermesso: "clienti", completion: { result in
                DispatchQueue.main.async {
                    if(result == "false") {
                       viewControllerList.removeAll(where:{ $0 is ClienteViewController })
                          self.viewControllers = viewControllerList
                    }
                }
            });





        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
4

1 回答 1

0

我想原因是这里对删除的不平衡调用

viewControllerList.remove(at: 1)
viewControllerList.remove(at: 5)

意思是当您删除索引 1 处的 say 元素时,如果数组计数为 6,这意味着当您尝试在 5 处删除时,它将崩溃,因为现在最大计数为 4


您可以避免在 remove ex 中使用索引而不是viewControllerList.remove(at: 0)

viewControllerList.removeAll(where:{ $0 is MarcaTempoView })
于 2019-05-28T10:31:08.337 回答