Last active
March 11, 2020 03:52
-
-
Save quangtqag/1139f090019401bb02477b21a2bf5ebb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func updateUser(id: Double, name: String, completionHandler: @escaping (_ userID: Double?, _ error: Error?) -> Void) { | |
let endpointUrl = URL(string: host + updateUserPath)! | |
var urlRequest = URLRequest(url: endpointUrl) | |
urlRequest.httpMethod = "PUT" | |
let userDict: [String: Any] = ["id": id, "name": name] | |
let userData = try! JSONSerialization.data(withJSONObject: userDict, options: []) | |
urlRequest.httpBody = userData | |
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in | |
guard error == nil else { | |
print("error calling PUT on \(updateUserPath): \(error!)") | |
DispatchQueue.main.async { | |
completionHandler(nil, error) | |
} | |
return | |
} | |
let userData = try! JSONSerialization.jsonObject(with: data!, options: []) as! [String: Double] | |
let userID = userData["id"] | |
DispatchQueue.main.async { | |
completionHandler(userID, nil) | |
} | |
} | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment