Alamofire 发送带键和多值的参数数组
Posted
技术标签:
【中文标题】Alamofire 发送带键和多值的参数数组【英文标题】:Alamofire sending parameter array with key and multiple value 【发布时间】:2019-09-18 04:59:41 【问题描述】:我需要在我的项目中这样做:
多个数组中的多个值的参数公共键
一些有相同问题但没有确切答案的链接,我总是看到有答案的帖子,比如使用自定义编码,就是这样。
https://github.com/Alamofire/Alamofire/issues/570
我有 4 个数组:
var imagesArray: [UIImage] = [], var DayOpreation: [String] = [],varDayOffStart: [String] = [], var DayOffEnd: [String] = []
这是我的代码:
let parameters : Parameters = [
"about": self.descriptionLabel.text!,
"address" : addressLabel.text!,
"country" : "1",
"state": "1",
"city" : "1",
"postcode" : self.postalCode.text!,
"policies": self.policiesLabel.text,
"longitude" : "",
"latitude" : "",
"available_24hours": "0",
"open_hour" : "09:00",
"closed_hour" : "18:00",
"operating_days[0]": "Senin",
"days_off[0][start]" : "2019-10-10",
"days_off[0][end]" : "2019-10-15",
]
let token = UserDefaults.standard.string(forKey: UserDefaultConstant.ACCESS_TOKEN)
let headers = ["key": "\(token!)"
]
let ImageData = UIImageView()
ImageData.image = UIImage(named: "rectangle-1")
let imgData = UIImagePNGRepresentation(ImageData.image!)!
Alamofire.upload(multipartFormData: multipartFormData in
multipartFormData.append(imgData, withName: "photo_profile[0]",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in parameters
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
,
to:"\(Endpoints.BASE)\(Endpoints.UPDATE_VENDOR)",
method: .post,
headers: headers,
encodingCompletion: encodingResult in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
if let data = response.data
guard let json = try? JSON(data: data) else return
let messageRoom = json["message"].string
print("listOfficialRoom== ",json)
case .failure(let encodingError):
print(encodingError)
)
【问题讨论】:
【参考方案1】:在 Alamofire 中使用参数,类似这样的
let daysOff = [String: Any]() // array of dictionaries for days off
for (i, day) in days.enumerated()
let dayOff: Parameters = [
"start": day.start,
"end": day.end
]
daysOff["\(i)"] = dayOff
let parameters: Parameters = [
"open_hour": "...",
"closed_hour": "...",
"days_off": daysOff
]
【讨论】:
你好@Duy Le,谢谢你的回答。请检查我的更新问题 能否提供错误信息或解释具体问题? 参数的类型是 [String : Any],所以当你写 "days_off[0][start]" : "2019-10-10" 时,我认为它不会将其识别为days_off 数组的元素。尝试构建一个单独的字典数组并将它们传递给 Parameter 并让我知道它是否有效。我通常使用这种技术,它对我有用。以上是关于Alamofire 发送带键和多值的参数数组的主要内容,如果未能解决你的问题,请参考以下文章