Convenient Swift Extensions and Protocols

Photo by Mounzer Awad on Unsplash

Protocols and Extension are fantastic tools to help us be more productive.
I’m going to share a list of useful yet straightforward extensions and protocols that might help you organize your code and/or be more productive.

Bool Extension

Now you will only need to call myBool.toggle().

Optional Extension

Now instead of unwrapping the boolean, you can use the unwrappedOrFalse property.

The same situation happens using other data types, especially when using Int (at least to me).

Now instead of unwrapping the value, you can use the unwrappedOrZero property.

You can even use this on custom objects like so:

Enums with rawValue is another data type that using this extension is really helpful.

And then you just need to call:

Cell Registrable Protocol

Just make your UITableViewCell subclass conform to this protocol and this changes to this:

Now besides making the use of the reuse identifier type safe, your implementation changes to this:


Follow me

iOS Engineer