我希望我的函数以简单字符串形式返回JSON HTTP响应。但是,当我将函数签名更改为具有'String'返回类型时,Xcode现在抱怨“ void函数中出现非预期的非空返回值” 有小费吗?在我看来,这似乎并不直观。我只想将我的JSON响应作为字符串。
func getResponseString() -> String {
let url = getRequestURL(origin: "test", destination: "test")!;
// Excute HTTP Request
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print("Error: No data to decode")
return "error:no data"
}
do {
let responseString = String(data: data, encoding: String.Encoding.utf8)
print("responseString = \(responseString)")
return responseString
} catch {
print(error)
return "error"
}
}
task.resume()
}