Search code examples
iosswift3alamofirexcode8

Heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional


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?


Solution

  • 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]