14

I have a POST body paramter like this:

{
  "id": 0,
  "name": "string",
  "contactInfo": "string",
  "message": "string"
}

So since i am using the Alamofire for posting the parameters i am describing the post body dictionary like this:

let body = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]

class func postUserFeedback(userID: Int, userName: String, contactInfo: String, message: String,completionHandler: @escaping (FeedbackResponse?) -> Void) {
    let body = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]
    request(route: .userFeedback, body: body).responseObject { (response: DataResponse<FeedbackResponse>) in

      response.result.ifSuccess({
        completionHandler(response.result.value)
      })

      response.result.ifFailure {
        completionHandler(nil)
      }
    }

  }

But i am getting the error like this: Error screenshot

What i am doing wrong in this syntax?

Chelsea Shawra
  • 1,593
  • 4
  • 22
  • 43

3 Answers3

29

If the type could not be inferred you have to annotate

let body : [String:Any] = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]

or bridge cast it:

let body = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message] as [String:Any]
vadian
  • 274,689
  • 30
  • 353
  • 361
4

You should add a [String: Any] explicit type to the variable.

let body: [String: Any] = [
    "id": userID, 
    "name": userName,
    "contactInfo": contactInfo,
    "message": message
]
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
0

This is because you are constructing your post request with mix of data type i.e

  1. String
  2. Int (You are trying to send id as Int 0)

{ "id": 0, "name": "string", "contactInfo": "string", "message": "string" }

So, you have to use [String:Any] during declaration. If you don't want to use a casting [String:Any] convert id type Int to String like below.

{
      "id": "0",
      "name": "string",
      "contactInfo": "string",
      "message": "string"
}

Same error could be occurred if you use one Bool param and others String param.

Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68