arrays - 为 Alamofire POST 请求 Swift 4 创建主体

标签 arrays swift dictionary post alamofire

在使用 Alamofire 发送 POST 请求时,我需要创建自定义正文的帮助。

我正在发送至 API 产品。有两种类型的产品。第一种类型只有数量,第二种类型 - 不同的数量 (size_id) 以及与每个 size_id 匹配的数量。

最终正文应如下所示:

"factory_id": "1"

"order_products[0][product_id]": "1"
"order_products[0][size_id]": "2"
"order_products[0][quantity]": "10"

"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"

"order_products[2][product_id]": "3"
"order_products[2][quantity]": "10"

这是我迄今为止所取得的成就:

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]()

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() {

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

我得到的结果是:

"factory_id": "1",

"order_products[0][product_id]": "1"
"order_products[0][size_id]": "2", 
"order_products[0][quantity]": "10",

"order_products[1][product_id]": "2",
"order_products[1][size_id]": "2",  
"order_products[1][quantity]": "10",

"order_products[2][product_id]": "3",
"order_products[2][size_id]": "2", 
"order_products[2][quantity]": "10", 

正如你所看到的,我几乎达到了预期的结果,但问题是它只添加了 quantity 字典的最后一个元素并省略了其他值。另外,我不知道如何为没有 size_id

的产品添加数量

另外,我知道将 for in 循环放在其他 for in 循环中并不是一个好的做法,但我是开发新手,这是最好的做法我想到的想法。

非常感谢您对这个问题的任何帮助,因为我已经与这个问题斗争了将近一周了。

非常感谢,祝周末愉快!

最佳答案

经过在网上学习和搜索,这里有一个解决方案来解决我们这里的问题。作为引用,原始帖子在这里 - Original Post

假设每个产品都必须拥有自己的数量:

我们可以定义这样的结构:

struct Product {  
    let id: String  
    let quantities: [(sizeId: String, quantity: Int)]?  
    let noIdQuantity: Int?  

    init(id: String, quantities: [(sizeId: String, quantity: Int)]) {  
        self.id = id  
        self.quantities = quantities  
        self.noIdQuantity = nil  
    }  

    init(id: String, quantity: Int) {  
        self.id = id  
        self.quantities = nil  
        self.noIdQuantity = quantity  
    }  
} 

通过上面的结构,我们只需要一个输入变量和一个输出变量:

// Define input `product with id` as an Array of `Product`  
let products = [  
    Product(id: "1", quantities: [("1", 10), ("2", 10)]),  
    Product(id: "2", quantities: [("1", 10), ("2", 10)]),  
    Product(id: "3", quantities: [("1", 15), ("2", 20)]),  
    Product(id: "4", quantity: 10),  
]  
// Output dictionary  
var body = [String: Any]()  

要将单个产品的条目放入字典中:

extension Product {  
    func formBody(_ index: inout Int, into body: inout [String: Any]) {  
        if let quantities = self.quantities {  
            for (sizeId, quantity) in quantities {  
                body["order_products[\(index)][product_id]"] = self.id  
                body["order_products[\(index)][size_id]"] = sizeId  
                body["order_products[\(index)][quantity]"] = quantity  
                index += 1  
            }  
        }  
        if let quantity = self.noIdQuantity {  
            body["order_products[\(index)][product_id]"] = self.id  
            body["order_products[\(index)][quantity]"] = quantity  
            index += 1  
        }  
    }  
}  

并按如下方式使用它们:

var index = 0  
body["factory_id"] = "1"  
for product in products {  
    product.formBody(&index, into: &body)  
}  
print("Body quantity - ", body)  
body.sorted {$0.key < $1.key}.forEach{print("\($0.key)=\($0.value)")} //For showing readable result, not for Alammofire body  

所以,最终的结果是:

factory_id=1
order_products[0][product_id]=1
order_products[0][quantity]=10
order_products[0][size_id]=1
order_products[1][product_id]=1
order_products[1][quantity]=10
order_products[1][size_id]=2
order_products[2][product_id]=2
order_products[2][quantity]=10
order_products[2][size_id]=1
order_products[3][product_id]=2
order_products[3][quantity]=10
order_products[3][size_id]=2
order_products[4][product_id]=3
order_products[4][quantity]=15
order_products[4][size_id]=1
order_products[5][product_id]=3
order_products[5][quantity]=20
order_products[5][size_id]=2
order_products[6][product_id]=4
order_products[6][quantity]=10

希望这对具有相同复杂结构或类似结构的人有所帮助。

关于arrays - 为 Alamofire POST 请求 Swift 4 创建主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780085/

相关文章:

php - 检查值是否在范围内 php

swift - 如何知道在(iOS 13+)configurationForMenuAtLocation 上为上下文菜单触发了哪个 UICollectionView 单元格

C# 处理多个 if/else if 语句的更好方法

c++ - 将堆对象分配给 std::map

python - 如何获取嵌套字典列表中所有键的路径

javascript - 将数组中的所有其他值移动到新数组中

c++ - 在 C++ 中使用 char 通过文件 I/O 读取文件

javascript - 如何在 jQuery 函数中传递数组或添加循环

ios - 我可以使用 TabBarItem 上的自定义事件点击来制作自定义 UITabBarController 吗?

swift - 在 DatePicker-swift 中限制小时和分钟?