Swift — Optional

Swift — Optional

Swift language introduced us to a new concept called?Optional. If you’re new to Swift the concept might seem unfamiliar or not intuitive at first, but?Optional?has its strengths and we’re here to explain them.

What is Optional?

The correct definition for?Optional ,?it’s an?Enum?with 2 cases, those 2 cases are:

No alt text provided for this image

Where none means the the value is empty — nil, and some means the value is actually containing something.

voilà — that’s the surprise hiding under this weird new thing we got called?Optional.

To be more clear, an?Optional?is a wrapper around the value, this wrapper can be one of two, either empty?(.None), or has something inside?(.Some(Wrapped)).

Let’s say the developer assign a new value to an?Optional?the basic constructor will be called, this constructor creates a new?Enum?with the value the developer assigned set into the?.Some?case.

On the other hand if the developer assigned a nil value to an?Optional,?the constructor will create an?Enum?with?.None?case. In the screenshot above we can see that?Optional?conforms the?ExpressibleByNilLiteral?Protocol, and that’s why it allows us to set a nil value into an?Optional.

Unwrapping

So, now that we understand what?Optional?is, how do we use it?

There are few ways to use an?Optional?value.

Unconditional unwrapping

Unconditional unwrapping can be achieved be using the character ‘!’. by using unconditional unwrapping we’re saying: “yes, I'm 100% sure there’s a value in that?Optional?and I want to use it”, example:

No alt text provided for this image

Optional unwrapping

Another way to unwrap an?Optional?value is to make a check before using the actual value if it’s there or not, example:

No alt text provided for this image

Swift 3.0 introduced us to Optional unwrapping which is basically the same as the snippet above, just a much better looking syntax, example:

No alt text provided for this image

The code snippet above takes the need to use the ‘!’ mark in order to unwrap the?Optional, because the acutal value is inside?unwrappedString?,?unwrappedString?is not an?Optional?but an actual string.

Guard

guard?statement was introduced in Swift 2.0, guard let’s you unwrap the Optional, but if it does not exist it forces you to execute a different code, meaning that the code beyond this point cannot run without this?Optional, example:

No alt text provided for this image

Conclusion

Swift introduced us to a new way of using values, the?Optional?which is a powerful object. Developers might get confused since it’s really easy to overlook?Optional?by using unconditional unwrapping, but when using?Optional?the right way it makes the code safe, easy to maintain and readable.


This is a brief explanation regarding?Optional?in Swift, further reading you can find?here

要查看或添加评论,请登录

Adi Mizrahi的更多文章

  • Swift 6.0 Actors: Eliminating Race Conditions!

    Swift 6.0 Actors: Eliminating Race Conditions!

    Hey Swift devs! If you're anything like me, you're always on the lookout for ways to make your code cleaner, safer, and…

  • iOS vs. Android: How to Decide?

    iOS vs. Android: How to Decide?

    Introduction In the dynamic landscape of mobile app development, choosing the right platform is a critical decision…

    4 条评论
  • iOS: Downsampling for Improved Performance

    iOS: Downsampling for Improved Performance

    Introduction When developing iOS applications, managing images efficiently is crucial for delivering a smooth user…

    3 条评论
  • Optimizing Mobile Apps for Extended Battery Life: Tips for Android and iOS Developers

    Optimizing Mobile Apps for Extended Battery Life: Tips for Android and iOS Developers

    Introduction Mobile devices have become an integral part of our daily lives, but their limited battery life remains a…

    3 条评论
  • WWDC Highlights for Developers

    WWDC Highlights for Developers

    WWDC 2023 took place on June 5th at Apple Park, featuring several significant announcements for developers. Here are…

  • Mobile Image Optimization

    Mobile Image Optimization

    Motivation As mobile developers, we want to build successful applications, rich applications, and visual applications…

  • Swift — guard and why?

    Swift — guard and why?

    So i’ve been asked multiple times why and when should I use the guard statement?The guard statement was first…

社区洞察

其他会员也浏览了