UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How do I know that URLSession.shared.data throws an Error in SwiftUI

Forums > Swift

Below code catches an Error object "error" when the server is not available:

    do{
        let (data, urlResponse) = try await URLSession.shared.data(for: request)
    } catch {
        print(error)
    }

When I read up on the documentation of URLSession.shared.data it is nowhere mentioned that it also throws an error (for URLSession.shared.dataTask for example it states explicitly that an error item is part of the returned values).

I mean the code is working but how would I KNOW that it returns an error object? That part is absolutely not transparent to me.

2      

The method signature for URLSession.data(for:delegate:) looks like this:

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

See that throws in there? That's what tells you the method throws errors.

2      

Thank you sir, makes sense! :)

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.