我在 for 循环中获取值。它正在崩溃

I am getting values in for loop. It is getting crashed

我在 for 循环中从 Array[String:Any] 类型的数组获取值,但在 for 循环的第一行时它崩溃了。

实际上我已经创建了一个数组 (Array[String:Any]) 类型,现在我正在获取这个数组的所有值以显示在 tableview 上,但是它在 for 循环中崩溃了。

var ticketArray = [Any]()
var addTypeTicket = [String:Any]()
var imagesArray = [Any]()
var imagesFinal = [String:Any]()


let ticketDetails = ["ticketName":txtTicketName.text!,
                     "numberOfTicketOnSale":txtFldTotalQuantityofTicket.text!,
                     "ticketPrice":txtFldPriceofTicket.text!,
                     "messageForTicketBuyers":txtviewMessage.text!,
                     "ticketGroupName":txtFldGroupName.text!] as [String : Any]

ticketArray.append(ticketDetails)
print(ticketArray)
addTypeTicket["addTypeTicket"] = ticketArray
print(addTypeTicket)
viewShadowTicket.isHidden = true
viewForMainTicketAlert.isHidden = true

for alltickets in addTypeTicket {
    let ticketName = (alltickets as AnyObject).object(forKey: "ticketName") as! String
    let Quantity = (alltickets as AnyObject).object(forKey: "numberOfTicketOnSale") as! String
    let ticketPrice = (alltickets as AnyObject).object(forKey: "ticketPrice") as! String
    let arr = structTickets(ticketName: ticketName, numberOfTicketOnSale: Quantity, ticketPrice: ticketPrice)
    self.arrayTickets.append(arr)
}
self.tableview.reloadData()
tableview.isHidden = false

我只想在 for 循环中获取值。

如果您没有在其他任何地方使用 ticketArray,则无需向该数组添加数据。您可以通过以下方式简单地将数据添加到 addTypeTicket 字典:

addTypeTicket["addTypeTicket"] = ticketDetails

然后你可以像这样获取数据:

if let addTypeTicketData = addTypeTicket["addTypeTicket"] as? [String: String] {
    let ticketName = addTypeTicketData["ticketName"]
    let quantity = addTypeTicketData["numberOfTicketOnSale"]
    let ticketPrice = addTypeTicketData["ticketPrice"]
    let arr = structTickets(ticketName: ticketName, numberOfTicketOnSale: quantity, ticketPrice: ticketPrice)
    self.arrayTickets.append(arr)
}

并使用 arrayTickets 作为 tableView's numberOfRowsInSection