A few Swift tricks that you might not know

Mike Pesate
4 min readNov 26, 2018

Over the years we all come across that one article, post or even video that shows us a couple of neat tricks that we love and continue to use from that point onwards. This is why I’ve decided to write down some of my favourites ones.

1. Avoiding Type Repetition:

If you are like me, and you like your code to be strong typed and avoid having the compiler infer what a property should be based on what’s being assigned to it. Then you will like this tip, since it will help you avoid having to type the class twice.

As you can see from the example above. We are able to still declare the type of the property but we don’t need to rewrite it during the instantiation. This tip also comes in very helpful if during a refactor we need to change the class of a property for, lets say, a subclass. If the init is the same, then all we need to do is just change the type and that will be all. The compiler will know the context and adjust the init correctly.

2. Swift Errors

One of the more tedious things I have found in life is having to come up with names for new errors in Swift. But recently I found a way around this and it has become one of my favourite things to…

--

--

Mike Pesate

Senior iOS Developer trying to write more and share more.