Alamofire multipart发送String数组

时间:2017-11-29 04:18:28

标签: swift alamofire multipart

我想发送多部分字符串数组。

ex)

let list = ["ffff","bbbb","cccc"] 
let params = [
    "a" : "a",
    "b" : "b",
    "list : list
]

a和b案例

for (key, value) in params {
    multipartFormData.append( value.data(using: String.Encoding.utf8)! , withName: key)    
}

但如何列出发送?

请帮助我。

1 个答案:

答案 0 :(得分:2)

现在你有两种方法可以做到:

1:将整个参数设为JSON

2:只将数组作为编码的JSON字符串。

在您的情况下,您可以使用 1:

let list = ["ffff","bbbb","cccc"] 

let data = try! JSONSerialization.data(withJSONObject: list, options: .prettyPrinted)

let jsonString = String(data: data, encoding: .utf8)!

let params = [
    "a" : "a",
    "b" : "b",
    "list : jsonString
]