ios 异类集合文本只能推断为“[String:Any]";如果有意这样做,请添加显式类型注解

pcrecxhr  于 2022-12-24  发布在  iOS
关注(0)|答案(2)|浏览(128)

我有一个POST主体参数,如下所示:

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

因此,由于我使用Alamofire来发布参数,因此我对发布主体字典的描述如下:

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

  }

但我得到这样的错误:

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

xxhby3vn

xxhby3vn1#

如果无法推断 * 类型 *,则必须注解

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

doinxwow2#

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

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

相关问题