异构 collection 文字只能推断为“[String : Any]”;如果这是有意的,请添加显式类型注释

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

我有一个这样的 POST body 参数:

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

因此,由于我使用 Alamofire posting 参数,所以我描述 post body 字典是这样的:

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)
      }
    }

  }

但是我收到这样的错误:

我在这个语法中做错了什么?

您应该向变量添加 [String: Any] 显式类型。

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

如果无法推断类型,您必须注释

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

或者桥投:

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