【问题标题】:Creating a body for Alamofire POST request Swift 4为 Alamofire POST 请求 Swift 4 创建主体
【发布时间】:2019-03-17 17:14:01
【问题描述】:

在使用 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 循环中不是一个好习惯,但我是开发新手,这是我想出的最佳主意。

对于这个问题的任何帮助,我将不胜感激,因为我已经与它斗争了将近一个星期。

非常感谢,周末愉快!

【问题讨论】:

    标签: arrays swift dictionary post alamofire


    【解决方案1】:

    在网上研究和搜索后,这里有一个解决我们这里问题的方法。供参考,原帖在这里 - 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
    

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

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 2016-11-25
      • 1970-01-01
      • 2017-04-03
      • 2020-02-10
      • 2018-11-10
      • 2015-11-23
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多