The migration has been nothing short of a nightmare for me. I have this old code from the previous version of 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")
}
})
But now Xcode is giving me an error saying "Ambiguous reference to member 'upload(_:to:method:headers)" but I don't know whether I can trust these error messages because Alamofire fire calls and now throwing thousands of errors, one example is that encoding: .json
is now JSONEncoding.default
but Xcode tells me the error is "Extra method in function call". So I tried the solution for most of the other errors which is to switch the method and url arguments
Alamofire.upload(url, method: .post, headers ...)
But that too doesn't work. How am I supposed to rewrite this to work with the new Swift/Alamofire?
There's an example in Alamofire's test suite: 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
}
)