Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 816
Chelsea Shawra Avatar asked Jul 23 '17 18:07

Chelsea Shawra


2 Answers

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]
like image 76
vadian Avatar answered Nov 20 '22 19:11

vadian


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

let body = ["id": userID, 
            "name": userName,
            "contactInfo": contactInfo,
            "message": message] as [String: Any]
like image 34
Tamás Sengel Avatar answered Nov 20 '22 20:11

Tamás Sengel