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

时间:2019-05-10 10:34:28

标签: ios swift nsarray

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

实际上,我已经创建了一个数组(Array [String:Any])Type,现在我正在获取此数组的所有值以显示在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循环中获取值。

1 个答案:

答案 0 :(得分:1)

如果未在其他任何地方使用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