我有以下数据格式上传图片。我在编码字符串数组时遇到问题,而且我还有字典中的bool数据。
发布词典
[“title”:title,“desription”:description,“privacy”:public,“status”:true,“ids”:[“1”,“2”,“3”]]
我的编码:
Alamofire.upload(multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(multiPartParams?["image"] as! UIImage , 0.5) {
multipartFormData.append(imageData, withName: multiPartParams?["parameter"] as! String, fileName: "image.jpg", mimeType: "image/jpeg")
}
if params != nil {
for (key, value) in params! {
multipartFormData.append(((value as AnyObject).data(using: String.Encoding.utf8.rawValue))!, withName: key)
}}}, to: url, method: .post, headers: DataHandler.sharedInstance.getHeader(contentType: false),
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
guard response.result.error == nil else { // error for eg : network unavailable
completion(nil, response.result.error as NSError?)
return;
}
let reponseObject = response.result.value as? [String:AnyObject?]
guard reponseObject != nil else { // reponse is nil
let error = NSError(domain: "No Data",
code: 404,
userInfo: [NSLocalizedDescriptionKey : "No Data Returned"])
completion(nil, error)
return;
}
if (reponseObject![ResponseKey.success.rawValue] as? [String : AnyObject]) != nil { // Successfully get response
debugPrint( reponseObject)
completion(reponseObject![ResponseKey.success.rawValue]??[ResponseKey.data.rawValue]! as AnyObject?, nil)
} else if (reponseObject![ResponseKey.error.rawValue] as? [String : AnyObject]) != nil { //server error
let responseData = Mapper<Response>().map(JSON: reponseObject![ResponseKey.error.rawValue] as! [String : AnyObject])
let error = NSError(domain: "",
code: (responseData?.code)!,
userInfo: [NSLocalizedDescriptionKey : (responseData?.message?[0])! as String])
completion(nil, error)
}
}
case .failure(let encodingError):
completion(nil, encodingError as NSError?)
print("error:\(encodingError)")
}
})
}