Programming Swift like Swift not Java or Objective-C

The last article was written around September 2015 and a lot has happened in the world of Swift since then including my world (both of which are for another time/article). However in the last couple of months I have been interviewing developers to work with us and the interesting fact that emerges from the technical test that they have taken is their code writing style.
Now this is not of major consequence or is depends entirely upon how you feel about using a particular language, it is not as trivial as where do you place curly brackets on a line.

Code Writing Styles

So rather than beat around the bush, let’s have a look at some very basic things that distinguish the code writing styles.

This is the most common code you will see, for two reasons. The first being “because the compiler said so” as the compiler suggests that you can unwrap a variable using the ! rather than use chaining with the ?.

The code above is seen if someone is particularly interested in error handling, otherwise what you would have seen would be

let value = someOptionalVar!.attribute

and then you would find crashes in your app and you would wonder what happened. I must admit that I have also been guilty of using the same when I started and have daringly used the implicit unwrapping quite often.

So what is the swift way?

This is the interesting conversation, because one of the developers asked me this very question and to my surprise I found that there is more than one way in Swift to manage this.

1. Using ‘Guard’

Plus the advantage of unwrapping the values to use through the scope of the function it is used in.

2. Using ‘if let’

3. Using ‘flatMap’

4. Using ‘switch’

Though this seems unnecessary (in this example) however this can be used for associated values and other cases where normal ‘if’ might not.


This is a rather small article and while Swift is changing and evolving, there will be an opportunity for more articles on both language features and styles. How do you write your code? I have heard so many candidates speak about swift and about using protocols and frameworks but never saw them using it. All of that for another day.

Views All Time
Views All Time
Views Today
Views Today
Posted in Uncategorized and tagged , , , , , , .