How can I run code after the return statement in a function in Swift?

Since Swift 2.0 we have the keyword called "defer". A keyword that allows us to specify a block of code, a segment inside our function that will be executed just before the program control is transferred outside of the scope. Maybe for cleanup or other needs, actions that need to happen even if an error is thrown.

The code execution inside a defer block is deferred until the penultimate statement is executed, assuming the case where the last one is a return statement.

Here's how you can use it:

func anyFunction(someParameter: Int) -> Int {

    // Some code

    defer {

        // Code to be deferred.

    }

    return someValue

} // anyFunction

The position of the defer block should be placed anywhere within the curly braces and always before the return statement, for logical reasons and also to avoid the warning: "Code after 'return' will never be executed".

Some examples:

enter image description here

enter image description here


The answer to your question is no. But the solution to your problem is simple.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let validator:NSPredicate = NSPredicate(format:"SELF MATCHES %@","[A-Za-z0-9- ]+")
    if(validator.evaluateWithObject(string) || string == "" /* i.e. backspace */) {
        DispatchQueue.main.async {
            print("about to process textField, timestamp: \(Date().timeIntervalSince1970)")
            self.process(textField)
        }
        print("about to return true, timestamp: \(Date().timeIntervalSince1970)")
        return true
    }
    else {
        return false
    }
}

DispatchQueue.main.async will defer execution to the next iteration of the run loop. With those print() statements you'll see this in the console. Your timestamps will be different but you'll see the small difference (about 15/1000 of a second in this case).

about to return true, timestamp: 1612578965.103658

about to process textField, timestamp: 1612578965.1188931

If you require a specific delay use DispatchQueue.main.asyncAfter

The best explanation I have found on this is Matt Neuburg's book "iOS 14 Programming Fundamentals with Swift". See the Delayed Performance section.