Alamofire上传JSON响应未编译



我正在做一个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)
}

任何想法?

既然您已经有了一个可编码/可解码的Community结构体,请尝试以下方法:

AF.upload(multipartFormData: { multipartFormData in  
//do upload stuff to the server here 
}, to: server)  
.responseDecodable(of: Community.self) { response in 
debugPrint(response)     
}

最新更新