if vs if let vs guard let in Swift

if let and guard let are two conditional operators or condition checker which make our life super easy. Other languages have only if as condition checker but swift provides if let as well as guard let also which are operationally the same but a bit different in functionality.

To understand their differences, let’s try to understand what they are in details first.

if condition

Normal if condition is nothing but to check whether a condition is true or not.

This can be clubbed with else and else if but both of them are optional.

we can even club multiple if conditions with simple && or || operators based on the use case.

if let

Now let’s think about someplace where we want to compute something and based on the computed value we need to put an if condition.

We are trying to check if an element is present in an array and if present we are trying to print its position.

The same code can be replaced with if let instead of if condition.

Please continue reading at https://agrawalsuneet.github.io/blogs/if-vs-if-let-vs-guard-let-in-swift/

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store