DEV Community


What are Optionals in Swift Anyway?

2spacemilk profile image Mark Harless ・3 min read

I have taken it upon myself to learn Swift, the programming language created by Apple for use on their devices. As if learning JavaScript, React, Python, Flask and Django isn't enough! I'll admit, I didn't know much about the language before I started it. I've seen sample code or screenshots posted online and they look so... confusing. It turns out, they are confusing but many of the code is autogenerated for you when you drag-and-drop an element into your file. Yep, I didn't know it was partly a visual language where you get to point and click on a simulated device to interact with it. Amazing!

I'm only a few hours into my course but I've already learned so much. Some things I like, somethings I don't like. One of those things I don't like is that you aren't able to reassign a variable to a different data type. What do I mean by this? If I were to create a variable called myName and set it to a some string, I can't reassign myName's value to, let's say, an integer. Or a float, double, array, etc. It can forever only be assigned to a string type. I hate it!

To top it off, you can't assign a variable the value of nil and reassign it to a primitive data type later. Lame! I know! This is problematic for me because I'm used to doing this all the time. In React, before any fetching occurs, I set my states to null. They can then easily be reassign to a string, array, object, etc. This isn't possible in Swift.

So what are the different methods to best combat optionals? There's if let, force unwrapping and optional chaining; but, what is recommended by many Swift developers is the guard let statement. This is the one I want to talk about. First, let's consider this single line of code:

var optionalNumber: Int?
Enter fullscreen mode Exit fullscreen mode

What it's saying is to create a variable called optionalNumber and assign it the Int? property. This means that it will either be an integer or a nil value. Obviously, it will never be both, but as I said previously, if it's one data type, it can never be the other.

Guard let statements are great because it provides an early return in a function should an optional be equal to nil.

func doubleNumber(num: Int?) {

  guard let num = num else { return }

  print("The doubled number is \(num * 2)")

Enter fullscreen mode Exit fullscreen mode

So what we're doing in this function is returning double the number that we pass in as an argument. If it's nil, however, it will exit the function early and not reach the print statement.

The second line of code is our guard let statement that catches an nil values. It does that by creating a temporary variable with the same name as number. If number is not nil it gets skipped. If it is, it will run the code within its curly brackets. Remember, the brackets are on one line but you can use it in a multiline to add more lines of code before the return statement.

And that's all there is to guard let and optionals! If you're interested in learning more about optionals, I suggest you take a look at the other ways you can use them which I provided above.


Editor guide