0

I'm trying to post an object like so:

{
  "simulation" : { },
  "simOptions" : [
    { },
    { }
  ]
}

and tried posting it by calling my SimulationRepository class:

SimulationsRepository().confirmSimulation(params: parameters) { (response) in
  if let error = checkError(response) {
    self.hideLoading()
    self.showAlert(error)
    return
  }

  guard let simsArray = SimulacaoArray(responseObject: response.result.value) else {        
    let error = response.error.debugDescription      
    self.hideLoading()
    self.showAlert(error)        
    return
  }

  print(simsArray.simulation.count)
}

I've tried the solutions here Sending json array via Alamofire and here Alamofire: Send JSON with Array of Dictionaries, but can't get the array of dictionaries to be converted properly.

Community
  • 1
  • 1
Frederico
  • 203
  • 3
  • 8
  • Not really as I need to POST an array of dictionaries... I can GET just fine. – Frederico Mar 28 '17 at 18:39
  • Check here then: https://github.com/Alamofire/Alamofire#post-request-with-json-encoded-parameters , or for duplicates: http://stackoverflow.com/questions/36204198/alamofire-send-json-with-array-of-dictionaries or http://stackoverflow.com/questions/30394112/how-do-i-use-json-arrays-with-alamofire-parameters or to find even more https://www.google.se/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=post+JSON+object+with+a+dictionary+and+an+array+of+dictionaries+using+alamofire&* . Please make sure you search for the question before asking. GL –  Mar 28 '17 at 18:42
  • Thanks, but I've been through all of those questions, none of those solutions worked for me... Stackoverflow is my 'last resort' not the first. You're being very presumptuous by assuming I haven't done so. – Frederico Mar 28 '17 at 19:18
  • Edit your question and update it with that you have read the documentation and the duplicates and why they didn't help you and what is different in your case, then nobody needs to be presumptuous. You need to atlest present the problem and what solutions you have tried, what errors you run into . Not let people read your pasted code and automagically guess what happens when you run it. Check here: https://stackoverflow.com/help/how-to-ask This will atleast give you a higher chance to receive an answer. Good luck. EDIT: I have retracted the duplicate flag. –  Mar 28 '17 at 19:25
  • 1
    Ok, thanks for the tip... I'll do that! – Frederico Mar 28 '17 at 19:29
  • can you add what you get as result, what you expected instead and which error? – muescha Mar 28 '17 at 19:42

1 Answers1

0

Ok I've solved by maping the simulated goods array and converting it to a dictionary, like so:

class SimulationsRepository: BaseRepository {

  init() {
    super.init(url: "/Simulations")
    super.BaseUrl = "http://0.0.0.0:3000/api"
  }

  func confirmSimulation(simulation: Simulation, goods: [SimulatedGoods], then: @escaping ThenBlock) {
    let goodsDict = goods.map { (simulatedGoods) -> [String: Any] in
      return simulatedGoods.toDictionary()
    }
    let sim = simulation.toDictionary()

    let params: [String: Any] = [
      "simulation": sim,
      "goods": goodsDict
    ]
    super.customPost("/confirm", parameters: params, then: then)
  }

}

By the way, I got the idea for maping the array like this from the book: https://www.hackingwithswift.com/store/pro-swift

Frederico
  • 203
  • 3
  • 8