Swift’s guard and throw statements encourage a style of programming where we return early from a given scope in case of error. Returning early from a given scope poses it’s own problems though – primarily how to ensure that any resources that were created within the scope are reliably cleaned up before exiting. In this post, we look at how to solve these types of a problem using Swift’s defer
Statement.