So one of the challenges in the CupcakeCorner wrap up section states:
If our call to placeOrder() fails – for example if there is no internet connection – show an informative alert for the user. To test this, just disable WiFi on your Mac so the simulator has no connection either.
And the code that Paul provided looked something like this:
URLSession.shared.dataTask(with: request) { data, response, error in
if let decodedOrder = try? JSONDecoder().decode(Order.self, from: data) {
self.confirmationMessage = "Your order for \(decodedOrder.quantity)x \(Order.types[decodedOrder.type].lowercased()) cupcakes is on its way!"
self.showingConfirmation = true
} else {
print("Invalid response from server")
}
}.resume()
The problem is, the code above actually does not work for me. Particulary, Xcode does not like the second line
if let decodedOrder = try? JSONDecoder().decode(Order.self, from: data)
. It pushed me to rewrite the line to,
if let decodedOrder = try? JSONDecoder().decode(Order.self, from: data!)
using forced unwrapping. So naturally, when I go to turn off the internet connection and place an order when trying to complete the challenge question, the app crashes and burns due to a nil value being discovered.
Did anyone else have a similiar problem to this? I've attempting trying to solve it using nil coalescing, but Swift doesnt like that either since 'String' and 'data' and two different types.
Thank you for taking the time to read this far! I really appreciate this community