Alamofire为多部件请求添加参数

时间:2017-01-16 17:55:31

标签: swift alamofire

我正在尝试向多部分请求添加参数,但没有成功,任何建议。

编译器说'Any'类型的值没有成员'data'

enter image description here

func createRep(_ question: String, type: String, anonymous: Bool, votesRequired: String, firstType: String, firstText: String, firstMedia: UIImage, secondType: String, secondText: String, secondMedia: UIImage, completionHandler: @escaping (APIResult<Any>) -> Void)
  {
    let parameters: Parameters = [
      JSONKey.question.rawValue: question,
      JSONKey.type.rawValue: type,
      JSONKey.anonymous.rawValue: anonymous,
      JSONKey.votesRequired.rawValue: votesRequired,
      JSONKey.firstType.rawValue: firstType,
      JSONKey.firstText.rawValue: firstText,
      JSONKey.firstMedia.rawValue: firstMedia,
      JSONKey.secondType.rawValue: secondType,
      JSONKey.secondText.rawValue: secondText,
      JSONKey.secondMedia.rawValue: secondMedia
    ]

    let headers: HTTPHeaders = [
      "Authorization": "Bearer " + keychainService.getToken(),
      "Accept": "application/json"
    ]

    let url = EndPoint.baseUrl.rawValue + EndPoint.reps.rawValue

    let urlRep = try! URLRequest(url: url, method: .post, headers: headers)

    Alamofire.upload(multipartFormData: { (multipartFormData) in

      for (key, value) in parameters {
        multipartFormData.append(value.data(using: .utf8)!, withName: key)
      }

      if let firstMediaData = UIImagePNGRepresentation(firstMedia), let secondMediaData = UIImagePNGRepresentation(secondMedia) {
        multipartFormData.append(firstMediaData, withName: "firstMedia", fileName: "firstMedia", mimeType: "image/png")
        multipartFormData.append(secondMediaData, withName: "secondMedia", fileName: "secondMedia", mimeType: "image/png")
      }

      }, with: urlRep, encodingCompletion: { encodingResult in
        switch (encodingResult) {
        case .success(let value):
          completionHandler(APIResult.success(value))
        case .failure(let error):
          completionHandler(APIResult.failure(error))
        }
      }
    )

}

0 个答案:

没有答案