Combining Swift’s Lazy Properties with the Builder Pattern

Alex Manarpies
iOS App Development
2 min readSep 15, 2014

--

For succinct deferred property initialization

My introduction to Apple’s new Swift programming language has been somewhat of a bipolar experience. Initially delighted by modern language features such as type inference and optionals, I rapidly became disappointed with the rigidity of its generics system and its lack of real reflection APIs. As I found out quite painstakingly, these limitations currently render Swift useless for more advanced types of projects (e.g. an IoC container). Remember that Swift is still very much 1.0 and will continue to evolve toward parity with Objective-C in the months to come. I’m not sure whether it’s wise to go full-Swift for production-grade applications just yet.

On a more positive note: the introduction of Swift brings language-level support for lazy loading of instance properties! When combined with the GoF’s Builder Pattern (with closures), it makes for an exceptionally succinct syntax.

Let’s take a look..

UPDATE: In Swift 2.0, a much simpler syntax using an anonymous closure is now probably better:

A better approach, using an anonymous closure, which doesn’t require an additional initializer

.. and start with an example of the end result:

https://gist.github.com/aceontech/050d2e53fe931df53431.js

At runtime, the first call to the mapView property will initialize its value and invoke the closure. Gone are the days of manual nil-checking a property’s backing ivar, and adding additional initialization code in the conditional if-case.

To make this work, you merely define an initializer which takes an optional closure (the builder). The MapKit example above worked because I added a convenience initializer to MKMapView using a Swift Extension:

https://gist.github.com/aceontech/8c0c682daa433862a2ed.js

An extension clearly isn’t necessary when dealing with your own classes, but it provides an easy way of tacking the Builder Pattern onto existing Cocoa Touch classes.

The if-let structure is part of Swift’s Optional language-feature. It simply checks the parameter’s validity and conditionally executes the code block when valid (i.e. the closure isn’t nil).

Is this really necessary? No. You could make do with regular initializer parameters. However, the closure-syntax allows for vertical property assignments and it provides a place for additional initialization local to the property being initialized.

Recommended

Take a look at ochococo’s GitHub repository “Design-Patterns-In-Swift”. It lists the Gang of Four’s software design patterns along with their implementations in Swift.

It’s a nice piece of work, albeit still somewhat incomplete.

Header photo courtesy of Pedro Moura Pinheiro on Flickr. Image licensed under Creative Commons CC BY-NC-SA 2.0. Thank you :-).

--

--

Alex Manarpies
iOS App Development

Freelance #iOS developer & co-organizer of @mobel_io developer/users group #Belgium.