このコードはプロジェクトごと https://github.com/m-tmatma/URLSessionTest.git で公開しています。
import Foundation class Request { let session: URLSession = URLSession.shared // GET METHOD func get(url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) { var request: URLRequest = URLRequest(url: url) request.httpMethod = "GET" session.dataTask(with: request, completionHandler: completionHandler).resume() } // POST METHOD func post(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws { var request: URLRequest = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted) session.dataTask(with: request, completionHandler: completionHandler).resume() } // PUT METHOD func put(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws { var request: URLRequest = URLRequest(url: url) request.httpMethod = "PUT" request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted) session.dataTask(with: request, completionHandler: completionHandler).resume() } // PATCH METHOD func patch(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws { var request: URLRequest = URLRequest(url: url) request.httpMethod = "PATCH" request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted) session.dataTask(with: request, completionHandler: completionHandler).resume() } // DELETE METHOD func delete(url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) { var request: URLRequest = URLRequest(url: url) request.httpMethod = "DELETE" session.dataTask(with: request, completionHandler: completionHandler).resume() } }Request を使うコード
let request: Request = Request() let url: URL = URL(string: "http://www.asahi.com")! request.get(url: url, completionHandler: { data, response, error in if let res = response { print(res) } if let dat = data { print(dat) } if let err = error { print(err) } })