Skip to content

Instantly share code, notes, and snippets.

@samuelowino
Created August 19, 2022 07:18
Show Gist options
  • Select an option

  • Save samuelowino/c8ed35c6edcc223896877e0d11ff26e4 to your computer and use it in GitHub Desktop.

Select an option

Save samuelowino/c8ed35c6edcc223896877e0d11ff26e4 to your computer and use it in GitHub Desktop.
Defer statements
import Foundation
enum BadRequestException: Error {
case NetworkRequestFailed(cause: String)
}
struct NetworkConnection {
var openStatus: Bool
}
func get() throws {
throw BadRequestException.NetworkRequestFailed(cause: "Api request failed for reasons...")
}
func makeApiRequest() {
var networkConnection = NetworkConnection(openStatus: false)
do {
networkConnection.openStatus = true
try get()
} catch {
print("Api request failed but connection was closed and update to \(error)")
}
if networkConnection.openStatus == true {
defer {
//close this connection regardless
networkConnection.openStatus = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment