Hajime Kuge Hajime Kuge - 29 days ago 15
Swift Question

Cannot call value of non-function type '((UInt) -> Data?)!' with Alamofire 4

I'm trying convert code for Swift 3 and Alamofire 4, and I'm currently struggling with the following error:

Cannot call value of non-function type '((UInt) -> Data?)!'


at this line:

multipartFormData.append(data: value!.data(using: String.Encoding.utf8.rawValue)!, name: key)


Please give your advice for this case. My current code is below.

Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData!, withName: "image", fileName: nowString + "To" + receiverString! + ".jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(data: value!.data(using: String.Encoding.utf8.rawValue)!, name: key)
}
},to:"uploadimgURL"
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _)
upload.responseString(completionHandler: { (response) in
debugPrint(response)
})

case .failure(let encodingError):
print(encodingError)
}
}
)

Answer

I do see one issue with the line in question. Try not using the raw value of the enum like so:

let stringValue = value as! String
multipartFormData.append(data: stringValue.data(using: .utf8)!, name: key)