func sendToGist(payload : GistExport.Output) {
guard let data = encode(src: payload) else {
print("no data")
return
}
var urlString = "https://api.github.com/gists"
if userProject!.gistID != nil {urlString += "/\(userProject!.gistID!)"}
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.httpBody = data.data(using: String.Encoding.utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/vnd.github+json", forHTTPHeaderField: "Accept")
request.setValue("Bearer \(StartController.token)", forHTTPHeaderField: "Authorization")
request.httpMethod = userProject?.gistID == nil ? "POST" : "PATCH"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
let data = data,
let response = response as? HTTPURLResponse, error == nil
else { // check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
do {
let decodedData = try JSONDecoder().decode(GistResult.self, from: data)
print("done \(decodedData)")
self.userProject!.updateCurrentID(decodedData.id)
} catch {
print(error) // parsing error
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
} else {
print("unable to parse response as string")
}
}
}
task.resume()
}
The term let task = URLSession.shared.dataTask(with: request) { data, response, error in guard
throws the error in the title. This code was running a couple of months ago, and I think there has been an Xcode update since then. Is this the cause of this error? It crops up in 3 places, with the very same general function of sending requests to GitHub, so I'm quite sure I didn't change code in three places....
Help and guidance appreciated.
Incidentally, not my code here and something I've been asked to look to for a new feature....