-
Notifications
You must be signed in to change notification settings - Fork 167
Gigs Project Done #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Gigs Project Done #150
Conversation
swift-student
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice start to this project, Norlan!
| do { | ||
| let encoder = JSONEncoder() | ||
| encoder.outputFormatting = .prettyPrinted | ||
| let jsonData = try encoder.encode(user) | ||
| print(String(data: jsonData, encoding: .utf8)!) | ||
| request.httpBody = jsonData | ||
|
|
||
| let task = URLSession.shared.dataTask(with: request) { (_, response, error) in | ||
|
|
||
| // Check for error first | ||
| if let error = error { | ||
| print("Sing up failed with error: \(error)") | ||
| completion(.failure(.failedSignUp)) | ||
| return | ||
| } | ||
|
|
||
| // Check for response and make sure is a 200 | ||
| guard let response = response as? HTTPURLResponse, | ||
| response.statusCode == 200 else { | ||
| print("Sign up was unsuccessful") | ||
| completion(.failure(.failedSignUp)) | ||
| return | ||
| } | ||
|
|
||
| completion(.success(true)) | ||
| } | ||
|
|
||
| task.resume() | ||
|
|
||
| } catch { | ||
| print("Error encoding user: \(error)") | ||
| completion(.failure(.failedSignUp)) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While there is nothing wrong with the way you wrote this, I would personally prefer to wrap the do catch block more directly around the line doing the try to make it clear what is going to throw.
@swift-student