我正在將 Alamofire 上傳到服務器,并希望解碼一些作為回應發回的 JSON。
AF.upload(multipartFormData: { multiPart in
//do upload stuff to the server here
}, to: server)
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of file
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON(completionHandler: { data in
guard let JSON = data.result.value else { return }
print("JSON IS \(JSON)")
//decode the JSON here...
})
在我保護 data.result.value 有一個值(從服務器發送的 JSON 回應)的那一行上,我得到一個“運算式型別不明確,沒有更多背景關系”。
從服務器發送 JSON 物件的代碼在 Node.js 端如下所示:
app.post('/createCommunity', upload.single('cameraPhoto'), async function (request, response) {
// do operations to get required variables
var returnObject = {
community_id: id,
title: title,
members: members,
image: imageURL
}
response.send(returnObject)
}
有任何想法嗎?
uj5u.com熱心網友回復:
由于您已經有一個可編碼/可解碼的Community結構,請嘗試以下方法:
AF.upload(multipartFormData: { multipartFormData in
//do upload stuff to the server here
}, to: server)
.responseDecodable(of: Community.self) { response in
debugPrint(response)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485999.html
標籤:javascript 节点.js 迅速 阿拉莫菲尔 alamofire 请求
