Could you show what you get exactly with the following print:
print(id)
print(size)
body = formBody(products: String(index), factoryId: "1", productId: String(value), size_id: id, quantity: size)
print("Body quantity - ", body)
I see nowhere "Body quantity - "
When you say this is the result you get: result of what, where ?
Looking at you loop:
- for (index, value) in productIds.enumerated() {
- for (id, size) in quantity {
- print(id)
- print(size)
- body = formBody(products: String(index), factoryId: "1", productId: String(value), size_id: id, quantity: size)
- print("Body quantity - ", body)
- }
- }
At line 1, you will enumerate index, value on 3 pairs:
0, "1"
1, "2"
2, "3"
Then for each you will loop through quantities, with just 2 values for (id, size)
id 2 size 10
id 1 size 10
Note: size is always 10 ?
So, at line 4, you will have the following
0, "1"
id 2 size 10
id 1 size 10
1, "2"
id 2 size 10
id 1 size 10
2, "3"
id 2 size 10
id 1 size 10
Then you build body
0, "1"
id 2 size 10
Body quantity - ["order_products[0][size_id]": "1", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "factory_id": "1"]
id 1 size 10
Body quantity - ["order_products[0][size_id]": "2", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "factory_id": "1"]
1, "2"
id 2 size 10
Body quantity - ["factory_id": "1", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "order_products[1][quantity]": "10", "order_products[0][size_id]": "2", "order_products[1][size_id]": "1", "order_products[1][product_id]": "2"]
id 1 size 10
Body quantity - ["factory_id": "1", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "order_products[1][quantity]": "10", "order_products[0][size_id]": "2", "order_products[1][size_id]": "2", "order_products[1][product_id]": "2"]
2, "3"
id 2 size 10
Body quantity - ["factory_id": "1", "order_products[2][size_id]": "1", "order_products[2][quantity]": "10", "order_products[2][product_id]": "3", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "order_products[1][quantity]": "10", "order_products[0][size_id]": "2", "order_products[1][size_id]": "2", "order_products[1][product_id]": "2"]
id 1 size 10
Body quantity - ["factory_id": "1", "order_products[2][size_id]": "2", "order_products[2][quantity]": "10", "order_products[2][product_id]": "3", "order_products[0][quantity]": "10", "order_products[0][product_id]": "1", "order_products[1][quantity]": "10", "order_products[0][size_id]": "2", "order_products[1][size_id]": "2", "order_products[1][product_id]": "2"]
What do you do with it ? How do you build finalBody ?
What you get is the last value of body,
"factory_id": "1",
"order_products[2][size_id]": "2",
"order_products[2][quantity]": "10",
"order_products[2][product_id]": "3",
"order_products[0][quantity]": "10",
"order_products[0][product_id]": "1",
"order_products[1][quantity]": "10",
"order_products[0][size_id]": "2",
"order_products[1][size_id]": "2",
"order_products[1][product_id]": "2"
but you have lost the previous ones, notably the one created at case 1, "2"
So, you should store body in finalBody
But with disctionary, you will not get 2 records as
"order_products[1][product_id]": "1"
"order_products[1][size_id]": "3"
"order_products[1][quantity]": "10"
"order_products[1][product_id]": "2"
"order_products[1][size_id]": "2"
"order_products[1][quantity]": "10"
you use the same key, with different values, so you erase the first value you stored.
For finalBody, you should use an array of dictionaries.
So at the end:
var createOrderBody = [String: Any]()
let productIds = ["1", "2", "3"]
var body = [String: Any]()
var quantity = ["1" : "10", "2": "10"]
var noIdQuantity = ["10"]
var finalBody = [[String: Any]]() // An array of dictionaries
func formBody(products: String, factoryId: String, productId: String, size_id: String, quantity: String) -> [String: Any] {
createOrderBody["factory_id"] = factoryId
createOrderBody["order_products[\(products)][product_id]"] = productId
createOrderBody["order_products[\(products)][size_id]"] = size_id
createOrderBody["order_products[\(products)][quantity]"] = quantity
return createOrderBody
}
for (index, value) in productIds.enumerated() {
print("\nindex, value", index, value)
for (id, size) in quantity {
print("id", id)
print("size", size)
body = formBody(products: String(index), factoryId: "1", productId: String(value), size_id: id, quantity: size)
print("Body quantity - ", body)
finalBody.append(body)
}
}
print("\nfinalBody", finalBody)
and you get finalBody
finalBody [[
"order_products[0][size_id]": "2",
"order_products[0][product_id]": "1",
"factory_id": "1",
"order_products[0][quantity]": "10"],
["order_products[0][size_id]": "1",
"order_products[0][product_id]": "1",
"factory_id": "1",
"order_products[0][quantity]": "10"],
["order_products[0][size_id]": "1",
"order_products[0][quantity]": "10",
"order_products[0][product_id]": "1",
"order_products[1][product_id]": "2",
"factory_id": "1",
"order_products[1][size_id]": "2",
"order_products[1][quantity]": "10"],
["order_products[0][size_id]": "1",
"order_products[0][quantity]": "10",
"order_products[0][product_id]": "1",
"order_products[1][product_id]": "2",
"factory_id": "1",
"order_products[1][size_id]": "1",
"order_products[1][quantity]": "10"],
["order_products[0][size_id]": "1",
"order_products[0][quantity]": "10",
"order_products[0][product_id]": "1",
"order_products[1][product_id]": "2",
"factory_id": "1",
"order_products[2][size_id]": "2",
"order_products[2][product_id]": "3",
"order_products[1][size_id]": "1",
"order_products[1][quantity]": "10",
"order_products[2][quantity]": "10"],
["order_products[0][size_id]": "1",
"order_products[0][quantity]": "10",
"order_products[0][product_id]": "1",
"order_products[1][product_id]": "2",
"factory_id": "1",
"order_products[2][size_id]": "1",
"order_products[2][product_id]": "3",
"order_products[1][size_id]": "1",
"order_products[1][quantity]": "10",
"order_products[2][quantity]": "10"]]
Is it what you are looking for ?