ios - How to upload parameters with non-string value in upload method in Alamofire -


my parameter [string:anyobjecy] dictionary , using alamofire upload method upload image. when want pass parameters it, using method suggested edfunke (https://stackoverflow.com/a/33012955/4919289) encounter following errors: 'nsinvalidargumentexception', reason: '-[__nscfnumber datausingencoding:

during:

for (key, value) in params{       print("before key: \(key)")       multipartformdata.appendbodypart(data: value.datausingencoding(nsutf8stringencoding)!, name: key)       print("after key:\(key)") } 

i guess because value in dictionaries not string, how can solve it?

updated @ 27/2/2017 swift 3

    let params = [         //your params     ] [string : any]     alamofire.upload(multipartformdata: { (multipartformdata) in         multipartformdata.append(self.user.imagedata, withname: "your file name", filename: "your file name extension", mimetype: "image/jpeg")         (key, value) in params{             print("key,value: \(key),\(value)")             let stringvalue = value as! string             multipartformdata.append(stringvalue.data(using: .utf8)!, withname: key)         }     }, to:"your url")     { (result) in         switch result {         case .success(let upload, _, _):              upload.uploadprogress(closure: { (progress) in                 //print progress                 print("upload progress: \(progress.fractioncompleted)")             })             upload.responsejson { response in                 //print response.result                 print(response.result)              }          case .failure(let encodingerror):             print("in faulure")             print(encodingerror)             self.presentalertview(title: "error", message: "cannot register", buttontext: "ok")             //print(encodingerror.description)             break             //print encodingerror.description         }     } 

if uiimage can this

for (key, value) in params{    if let image = value as? uiimage {       if let imagedata: nsdata = uiimagejpegrepresentation(image, 1){           multipartformdata.appendbodypart(data: imagedata, name: "file", filename: "key.jpg", mimetype: "image/jpg")       }   } else {       multipartformdata.appendbodypart(data: value.datausingencoding(nsutf8stringencoding)!, name: key)   }  } 

Comments