I am getting values from and Array[String:Any] type array in a for loop but it is getting crashed when going on first line in for loop .

Actually I have created an array (Array[String:Any]) Type and now I am getting all the values of this array to show on tableview but it is getting crash in for loop.

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

I just want to get values in for loop .

1 Answers

1
Sharad Chauhan On Best Solutions

If you are not using ticketArray anywhere else then there is not need to add data to that array. You can simply add data to addTypeTicket dictionary by:

addTypeTicket["addTypeTicket"] = ticketDetails

Then you can fetch the data like this:

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)
}

And use arrayTickets for tableView's numberOfRowsInSection.