When to use a defer statement
suggest changeA defer statement is a block of code that runs when the current scope exits. Use it for cleanup. condition and getOutputStream() are assumed.
As Swift’s guard statements encourage a style of early return, many possible paths for a return may exist. A defer statement provides cleanup code, which then does not need to be repeated every time.
It can also save time during debugging and profiling, as memory leaks and unused open resources due to forgotten cleanup can be avoided.
It can be used to deallocate a buffer at the end of a function:
func doSomething() {
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: 42)
// this pointer would not be released when the function returns
// so we add a defer-statement
defer {
data.deallocate()
}
// it will be executed when the function returns.
guard condition else {
return /* will execute defer-block */
}
} // The defer block also runs at the end of the function.
It can also be used to close resources at the end of a function:
func write(data: UnsafePointer<UInt8>, dataLength: Int) throws {
var stream:OutputStream = getOutputStream()
defer {
stream.close()
}
let written = stream.write(data, maxLength: dataLength)
guard written >= 0 else {
throw stream.streamError! /* will execute defer-block */
}
} // the defer block also runs at the end of the function
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents