0
 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....

Dan Donaldson
  • 1,061
  • 1
  • 8
  • 21
  • The question was, "what is the cause of this error", with the range being a change to Swift, Xcode or something else. The "duplicate" doesn't answer this question. – Dan Donaldson May 09 '23 at 03:27
  • I'd suggest to comment the whole line and its closure, and start rewriting it manually and let autocompletion help you? Are you using some extension of `URLSession`, some strange protocol like in the linked question? Some libs? – Larme May 09 '23 at 09:42
  • No, I'm not. The key seemed to be an explicit cast to request as URLRequest and including the completionHandler:, neither of which needed to be done before. My question was trying to find out if this singled any kind of general change to URLRequests. Code now compiiles, but haven't had a chance to run anything that calls it yet... it's all just calling the standard Github API. – Dan Donaldson May 09 '23 at 14:36

0 Answers0