Let’s say we want to share a NSDateFromatter instance in the scope of a class, for example, a UITableViewDataSource. We would declare it like this:

classMyTableViewController:NSObject,UITableViewDataSource{letdateFormatter:NSDateFormatteroverrideinit(){self.dateFormatter=NSDateFormatter()super.init()}// protocol implementation skipped, not relevant in this example}

As you can see, we need to initialize dateFormatter in init(). And to be sure the formatter is correctly configured, we would need to do it there too:

I like to have tiny functions with explicit names.
But, as of today, we cannot factorize all of this in a method of MyTableViewController because self is not initialized. We need to split instanciation and configuration:

And we need to do that for every property… not very practical.
To prevent cluttering up init() with all the property initialization values we could do that when we declare dateFormatter:

classMyTableViewController:NSObject,UITableViewDataSource{letdateFormatter=NSDateFormatter()overrideinit(){super.init()self.configureDateFormatter()}funcconfigureDateFormatter(){self.dateFormatter.dateStyle=.NoStyleself.dateFormatter.timeStyle=.FullStyle}// protocol implementation skipped, not relevant in this example}

This is better and the property type is now inferred.

But I am not quite satisfied with this.

Isn’t there a way to do this in Swift without declaring and calling configureDateFormatter()? Which is, by the way, only called once during the lifetime of the object?

Closures!

Here is how:

classMyTableViewController:NSObject,UITableViewDataSource{letdateFormatter:NSDateFormatter={letdateFormatter=NSDateFormatter()dateFormatter.dateStyle=.NoStyledateFormatter.timeStyle=.FullStylereturndateFormatter}()// protocol implementation skipped, not relevant in this example}

As you can see, we do not need to override init() anymore! And we have all dataFormatter initialization and configuration in one place.