gpt4 book ai didi

多个异步调用导致 Swift 崩溃

转载 作者:行者123 更新时间:2023-11-30 10:43:16 25 4
gpt4 key购买 nike

当源代码在以下位置执行时,下面的应用程序崩溃: u.VerificaPermesso(...),应用程序检查n个调用节点的其余服务器。代码是正确的,但是崩溃了,你能解释一下原因吗?调用剩余的 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()
}

}

最佳答案

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

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

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

<小时/>

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

viewControllerList.removeAll(where:{ $0 is MarcaTempoView })

关于多个异步调用导致 Swift 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56340150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com