-1

I have an array like below, which contains an Array called "OrderDetails", which is I think a dictionary not array(I'm not sure what to call it.
in another vc, I have to create OrderDetails and then put it here in dataa. In the other VC that I have to create OrderDetails, I have a table view, which means the data of the table (some of them) should be set to the array. could somebody help me how should I make that array?

 let dataa = [            "Id":0,
                                 "CustomrId": 111,
                                 "PriceVariableId": item[1].priceVariableIdCore,
                                 "PaymentTypeId":item[1].paymentVariableIdCore,
                                 "RefCo":item[1].refcoCore,
                                 "OrderNo":1122,
                                 "ReciverId":0,
                                 "DlvProvinceId":UserDefaults.standard.string(forKey: constantAddress.stateId),
                                 "DlvCityId": UserDefaults.standard.string(forKey: constantAddress.cityId),
                                 "DlvAddress": UserDefaults.standard.string(forKey: constantAddress.addressLine),
                                 "DlvZip": UserDefaults.standard.string(forKey: constantAddress.postalCode),
                                 "DlvTel": UserDefaults.standard.string(forKey: constantAddress.telephone),
                                 "DlvMobile": UserDefaults.standard.string(forKey: constantAddress.mobile),
                                 "TtIsSingle": TAK,
                                 "TtIsDouble": JOFT,
                                 "TtIsTrailer":TREILI,
                                 "Description":EntTozihat.text,

                                 "OrderDetails":["OrderDetailId":0 ,
                                                 "Qty":0,
                                                 "UnitPrice":600]

                                 ]

"OrderDetails":[("OrderDetailId":0 ,"Qty":0,"UnitPrice":600),(),()]

update:

I'm creating the OrderDetails like below:

    for _ in 0...item.count {

            let dict = ["Id": 0,
                        "Qty":item[indexPath.row].quantityCore ,
                        "UnitPrice":item[indexPath.row].feeCore ,
                        "GoodId":item[indexPath.row].goodIdCore,] as [String : Any]
            listArray.append(dict)

        }
        print("JACK=  \(listArray)")

I have to problem here!!! 1) I'm using this right into the "cellForRowAt" with "dequeueReusableCell" which means as many ro as I have it would return the data like below:

JACK= [["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0], ["Id": 0, "GoodId": 0, "UnitPrice": 0.0, "Qty": 0.0]]

the print() you see above print 4 time becuase I have 4 row!!!

the other problem 2) is that I don't know why it return that much data in each "JACK="

first: I need to know how to use the code outside of the "cellForRowAt" second:why I get thismuch data in "JACK"

Am1rFT
  • 227
  • 5
  • 18
  • Duplicate: Array from dictionary values: https://stackoverflow.com/questions/26988167/swift-dictionary-get-values-as-array Array from dictionary keys: https://stackoverflow.com/questions/26386093/array-from-dictionary-keys-in-swift – Kamil.S Oct 01 '18 at 08:07
  • The `dataa` is the dictionary and `OrderDetails` seems dictionary as well. You can get the values: `dataa["OrderDetails"]?.map({ $0.value })` – Shalva Avanashvili Oct 01 '18 at 08:09
  • @ShalvaAvanashvili what does dataa["OrderDetails"]?.map({ $0.value }) do – Am1rFT Oct 01 '18 at 08:10
  • @ShalvaAvanashvili updated the question – Am1rFT Oct 01 '18 at 08:12
  • @Kamil.S actually I have an array and the elements are supposed to be a dictionary – Am1rFT Oct 01 '18 at 08:16
  • @ItanHant `dataa` in your current code snippet is a dictionary not an array. – Kamil.S Oct 01 '18 at 08:20
  • Yes the main one is a dictionary, but my question is about the OrderDetails – Am1rFT Oct 01 '18 at 08:22
  • @ItanHant `dataa["OrderDetails"]?.map({ $0.value })` it will extract the values from the OrderDetails as an array. Then you will use that array in your tableview. – Shalva Avanashvili Oct 01 '18 at 08:23
  • I don't want to use it in table view! I want to put the data from table view into the array! – Am1rFT Oct 01 '18 at 08:24
  • First, it looks like this data is used many times. Why not creating a struct for it. Second, as pointed out correctly, this is a dictionary of type `[String: Any]`. You can simply get the order details by accessing it on it’s key: `guard let dataa[“OrderDetails”] as? [String: Int] else { // do something to fail, e.g. throw }`. – cr0ss Oct 01 '18 at 19:31

1 Answers1

1

Do it like so:

 let orderDetails = dataa["OrderDetails"]!

If you're not sure whether that entry exists or not you could unwrap it safely:

if let orderDetails = dataa["OrderDetails"] {
    //use orderDetails
}

Or if you need it in the rest of the scope:

guard let orderDetails = dataa["OrderDetails"] else {
    fatalError("Couldn't unwrap the order details")
}
//use orderDetails

That way orderDetails would be a dictionary of type [String: Int].

ielyamani
  • 17,807
  • 10
  • 55
  • 90
  • You should not use force unwrapping, but rather `guard` or work with optional types instead – cr0ss Oct 01 '18 at 19:27
  • It is on purpose, yes – cr0ss Oct 01 '18 at 19:32
  • Should be accepted as answer. For me the only “better” way for order information would be storing it into a struct, but that’s way beyond the question. – cr0ss Oct 01 '18 at 19:36
  • @cr0ss Yes it does look like a struct is a better way to structure the data. generally speaking, using dictionaries of type [Type1: Any] is a sign of bad data-modeling. – ielyamani Oct 01 '18 at 19:39
  • here you are telling how to put OrderDetails in dataa, my problem is how to create the OrderDetails – Am1rFT Oct 01 '18 at 20:49
  • @ItanHant actually this gets that array from the `dataa` dictionary. – ielyamani Oct 01 '18 at 20:51
  • so I first I need to create the Array and then implement it in to the dataa – Am1rFT Oct 01 '18 at 20:56
  • I just added something to the question , pls check that to to clarify my question – Am1rFT Oct 01 '18 at 20:57