LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Cupcake Corner 2nd challenge - please some hints

Forums > 100 Days of SwiftUI

hi,

i have problem with second challenge in Cupcake Corner.

Can you give me some hints where to start or what should i use? Do i have to add another catch block? Create some enum with cases? It has to me specific error to no intenret conection or if function fails?

"If our call to placeOrder() fails – for example if there is no internet connection – show an informative alert for the user. To test this, try commenting out the request.httpMethod = "POST" line in your code, which should force the request to fail. "

3      

Hi! Most probably you already have this line of code in the project.

let (data, _) = try await URLSession.shared.upload(for: request, from: encoded)

if you option + click on upload in the pop up help you will notice that this function can throw error

func upload(
    for request: URLRequest,
    from bodyData: Data,
    delegate: (URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)

which means that you need to wrap it in do catch block. So if there are errors thrown you can use the error and show it to the user. Errors already "pre-defined" so you don't really need to create your own.

So make the long story short in catch block you can use that error and assign it to a variable and then use another variable to trigger the alert.

catch {
            errorMessage = "Sorry, checkout failed.\n\nMessage: \(error.localizedDescription)"
            showingError = true
        }

so when there is an errro you can catch it and toggle showingError message and it will be shown to the user.

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.