Some safety commands in Swift!

Tram Ho

Several ways to help iOS apps prevent crashes during development and usage from the code stage

Always consider the default

Swift provides developers a very convenient way to pass a value while using a variable, in case it’s nil. See the example below:

Did you spot the problem? While this is considered a novice mistake, it can also happen to more experienced developers. In case you didn’t notice it, it’s a forced unwrap response

response as! Int

Even if you are sure that the response will always be there, there will be times when crashes and apps crash.

Chances are, it could happen because you or someone else will change in the future. An attribute or a response may now be required, but in the future your logic may change and display that property or have no value. So when that change goes into effect, chances are your app will crash in bulk! Here’s what you should do to avoid such disasters:

That ?? This is called the nil coalescing operator . Instead of checking if a variable is nil and if so, assigning it a value, the nil binding operator can do it in one line of code. It actually checks if the expression on the left produces the value and if it does, it assigns the value variable you supply on the right. If not, it will safely open the variable.

Use the if-let & guard-let type

While the nil binding operator is very useful, sometimes you need a little extra logic to calculate. For example, in case you know that a value can sometimes be 0 and sometimes not, you should use the if-let statement.

For example:

Although the above scenario seems more secure than force unwrap, it can still cause problems. What if the response is actually not a zero, but for some reason it’s not an Int anymore?

Use if-let

That way, you can specifically test what you expect, in the safest way possible! Of course, you can always add more checks in the same if-let statement, like so:

The if-let statement will only be considered valid if all of the sub-statements are not nil. Obviously, you can add else-if-let and else statements as needed.

In the above title, I mentioned another type of statement, the guard-let statement. That, not declaring a variable to be used, but that’s a plus. You will find it more useful in cases where you want to completely stop the line of code for a function or a block below it.

In this example, the updateUI function will only be called if the guard-let statement is not returned. If you just want to check if a variable contains a value without storing it somewhere, and otherwise return it, then you can do this:


  • The nil – ?? binding operators, allow you to provide a default value for a variable in case it is nil. You should use it in situations where you are always expecting a value.
  • The if-let statement, which helps you to safely unwrap options that may or may not have a value.
  • The guard-let statement is best suited for situations where you need to exit block / func when a variable or set of variables equals nil or doesn’t match the tests you provide.
Share the news now

Source : Viblo