0

迁移对我来说简直就是一场噩梦。我有以前版本的 Swift/iOS/Alamofire 的旧代码

let intVal = 0

Alamofire.upload(.POST, url, headers: ["StringValue": intVal, "StringValue2": "StringValue3"], multipartFormData: { mpfd in
        let image = self.profileImageView.image!
        let imageData = UIImageJPEGRepresentation(image, 0.8)!
        mpfd.appendBodyPart(data: imageData, name: "image", fileName: "custom_image.jpg", mimeType: "image/jpeg")
        }, encodingCompletion: { result in

            switch result {
            case .success(let request, _, _):
                let response = request.response
                print("response from image change: \(response)")
                print("Successfully changed pro pic")
            case .failure/*(let encodingError)*/:
                print("Failed to change pro pic")
            }

    })

但是现在 Xcode 给了我一个错误说“对成员'upload(_:to:method:headers) 的模糊引用”但我不知道我是否可以相信这些错误消息,因为 Alamofire 触发调用并且现在抛出了数千个错误,一个例子是encoding: .json现在JSONEncoding.default,但 Xcode 告诉我错误是“函数调用中的额外方法”。所以我尝试了大多数其他错误的解决方案,即切换方法和 url 参数

Alamofire.upload(url, method: .post, headers ...)

但这也行不通。我应该如何重写它以使用新的 Swift/Alamofire?

4

1 回答 1

1

Alamofire 的测试套件中有一个示例:https ://github.com/Alamofire/Alamofire/blob/9688b16f0546b97b16c775c75f42b3f4bfacc78e/Tests/UploadTests.swift#L244

guard let image = self.profileImageView.image,
  let imageData = UIImageJPEGRepresentation(image, 0.8) else {
    return
}

Alamofire.upload(
  multipartFormData: { multipartFormData in
    mpfd.append(imageData, withName: "image", fileName: "custom_image.jpg", mimeType: "image/jpeg")
  },
  to: url,
  headers: ["Header": value, "Another_Header": value2],
  encodingCompletion: { result in
    // Whatever
  }
)
于 2017-01-16T17:38:40.443 回答