A “What’s wrong with this Swift code?” problem

Paul Ardeleanu
Paul Ardeleanu’s Blog
2 min readOct 3, 2016

Swift is a really great concise language. Together with Xcode Playgrounds, it makes for a great tool to use during interviews, both as an interviewee and as a candidate.

Here is a simple “what’s wrong with this Swift code” interview problem I stumbled across; it highlights a side effect of computed class variables:

How to use it

Start with a playground with the code above (available as a gist here) and observe how the candidate’s approach to understanding why the label is not in white on a blue background like below.

If stuck, ask why label.superview is nil after “being added as a subview”:

Solution

Here is the solution:

Hope you’ve found this useful.

--

--

Paul Ardeleanu
Paul Ardeleanu’s Blog

iOS & Web engineer, trainer and coach - with a dash of UX & UI.