I did a quick Google search of “assign swift closure as variable” and got a bunch of result of people asking about this and many answers that were informative and downright useful. Of course, I’ve been writing Swift code for a year and I already know most of the answers so I just skimmed the results. I don’t even recall what made me want to do this search. But after I did it, I wondered what was the left amount of code needed to assign a closure to a variable.

For non-Swift programs, a Closure is a bit of code in curly braces that can be passed to functions, assigned to variables, and executed when needed. The name “Closure” is a Swifty thing and these block of code are also called Lambdas, code blocks, anonymous functions, etc., in other languages.

So here’s what I wrote in my code to see if it would work:

let derp = { print( "hello world" ) }
derp()

Without count the characters for “hello world”, this is pretty much as minimal you can get. This might even be useful code if the block contains code that will run in more than one or two places after this. For instance, a longer more “useful” function might include this code:

let derp = { print( "hello world" ) }
if date > expiration {
	derp()
}
let x = y + 5
getOtherStuff()
if x > 22 && other == "test" {
	derp()
}

This code would normally require that derp() be a function outside of this code. But derp() could use variables from within this code and passing those variables to another external function might be a problem. of course, it’s not a problem and this is probably terrible code! There is probably one situation where it is the best way to define a function and call it, but I doubt I would ever do this.

What about Swift Closures that really do make sense? Here is a bit of code that I wrote recently; Not shown is the definition of the SegueHandler class and the peformSegue function in it. You can tell from this call that a closure is used so that the singleton SegueHandler class knows how to prepare for the segue later during a callback:

		SegueHandler.shared.performSegue( from : self, withIdentifier: "toFormDetail", sender: self ) {
			segue in
			guard let vc = segue.destination as? FormDetailVC else { return }
			vc.form = form
			vc.formSaveId = loadSaveId
		}

Let me explain more…
The typical peformSegue() function in Swift on iOS takes parameters that tell the OS where to “go” during the segue. When that call is made, the OS calls back a function called prepare() that can be used to make changes to the destination view controller. But what if the code has ten segues with some of them being to the same view controller but with different data? That would be a nightmare to manage in the prepare() function. My code simply stores the closure in a variable in the singleton SegueHandler object and then when prepare() is called, my code calls a prepare() function in the SegueHandler class. The SegueHandler class then calls the closure code to set up the view controller. The beauty of this is that the view controller setup is done in a piece of code that is right there in the source file where the performSegue() is called!

I didn’t mean to explain a good use of a closure when I wrote this post. I was just toying with the idea of writing the most minimal closure possible. In fact, I wrote a post just a few weeks ago about the SegueHandler and all of the code is available there.