Skip to content

Instantly share code, notes, and snippets.

@mahmudahsan
Created December 22, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmudahsan/f8855b576a0b9d04e2a2a802a6b4cda5 to your computer and use it in GitHub Desktop.
Save mahmudahsan/f8855b576a0b9d04e2a2a802a6b4cda5 to your computer and use it in GitHub Desktop.
cleanup actions using defer
func readDesc(_ desc:inout String) throws {
let newDesc = desc
defer {
desc = ""
}
throw SampleError.sample2(message: "cool error")
print(newDesc)
}
var desc = "Life is cool and play is a tool!"
try? readDesc(&desc)
print("Empty: \(desc)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment