TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Debugging Guard Let Try?

Forums > SwiftUI

I'm trying to understand how I can get visibility on one of the values in the middle of a guard, let, try? block cribbed almost exactly from the Apple Earthquakes project:

    guard let (data, response) = try? await request),
          let httpResponse = response as? HTTPURLResponse,
          httpResponse.statusCode == 200
    else {
        logger.debug("Failed to receive valid response and/or data!")
        throw PollError.missingData

I needed to modify it slightly because I am sending a POST request to my API (unlike the Earthquakes demo), but I don't think that changes anything really.

If I'm understanding this correctly, in plain English, this code says: Wait for the results of the post request and store the httpResponse from the server in the response variable and the data returned by the server in the data variable. Then if we get a response, check to see that the server returned a 200 OK status code. If the POST didn't work or the data returned isn't valid (or is missing) or if the response code is not 200, then throw an error.

My problem is that -- for reasons I cannot comprehend -- I suspect the web server is sending back a 404 response because there's a CORS issue with the favicon.ico file. Obviously that has nothing to do with SwiftUI or my app, but I'm having a hell of a time debugging this to see if that is, in fact, the case.

How can I refactor this code to print() or otherwise log what the status code response is from the server?



You could try something like this:

guard let (data, response) = try? await url),
      let httpResponse = response as? HTTPURLResponse
//              httpResponse.statusCode == 200
else {
    logger.debug("Failed to received valid response and/or data.")
    throw QuakeError.missingData


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.