What is keyword defer in Swift

· 1 min read
What is keyword defer in Swift

The defer keyword in Swift is used to execute a block of code at the end of the current scope, regardless of how the scope is exited. The defer block is executed after any return statements, and before any finally blocks.

Here is an typical example of how to use the defer keyword in Swift:

func processFile(filename: String) {
    let file = openFile(filename)
    defer {
        closeFile(file)
    }
    // ...
    // code to process the file
    // ...
}

In this example, the processFile function opens a file using the openFile function, and then it registers a defer block to close the file using the closeFile function. The defer block is executed at the end of the processFile function, regardless of how the function is exited.

The defer keyword can be useful for cleaning up resources, such as closing files or releasing memory, or for performing any other tasks that need to be done before the current scope is exited. It can also help you avoid common pitfalls, such as forgetting to close a file or release a resource, or writing complex and error-prone cleanup code.

Running order

It is also quite important to point it out that the defer blocks are executed in the reverse order in which they are declared, and they are executed only once, even if the current scope is exited multiple times. This can affect the order and behavior of the defer blocks, and it is important to consider these factors when designing and implementing your code. Take a look at the example:

var a = "test"
func processWords(str: inout String) -> String {
  str = "new string" + "."
  defer {
    str = "another new string1 "
  }
  return str
}
print(processWords(str: &a))
print(a)
// output:
new string.
another new string1 

defer 's block code will run after the any return, take another look on this piece of code:

var a = "test"
func processWords(str: inout String) -> String {
  str = "new string" + "."
  return str
  defer {
    str = "another new string1 "
  }
}
print(processWords(str: &a))
print(a)
// output:
new string.
new string.

"another new string1 " is not print out due to the defer is even not declared before the return statement.