Understanding May Be Optional

by | February 12, 2019

Understanding May Be Optional

Jeff Summers

February 12, 2019

Programming help can come from the most unexpected places, you just have to pay attention to your surroundings. For Day 12 of the 100 Days of Swift challenge we are delving into the world of Optionals. Optionals, as their name implies, are attributes that may or may not have a value.

A real world example of an Optional may be middle name. Some people have one and some people don’t. Because of this you cannot require your program to capture the middle name. At other times a value may always be pertinent but at the time you are gathering the information it may not be known to the user. For example, if a family history program asks for your great-great-great grandmother’s maiden name, she may have one but you might not know it at that time so the data attribute is appropriate, it just may need to be left unfilled until you can get that information.

The thing with Optionals is that they could have a value. In our example above maybe we do know what our great-great-great grandmother’s maiden name is at which point we can enter it during data entry. In this case the field would contain a string value with the name. But if we don’t have the value at the current time, that field could have no value which Swift describes as nil. It is important to note that nil is not the same as a blank string or a string with just a space. Nil is the absence of all value.

This concept may be hard to remember (at least it was for me) until I happened to be in the doctor’s office today waiting to see the doctor about this cough, I have that won’t seem to go away. While in the waiting room the office had background music from one of the local oldies station. Sadly, the oldies station is now playing music that I can remember when I was a kid which doesn’t do much for making me feel any younger.

I was sitting in the office pondering Swift Optionals (perhaps I am sicker than I thought) when a Billy Preston song came on from 1974. That song was “Nothing From Nothing” which is a catchy tune that tends to stick with you for the rest of the day but it is also a great reminder of how Optionals work.

The lyrics of the song goes something like, “Nothin’ from nothin’ leaves nothin’, you gotta have somethin’ if you want to be with me.” Clearly Billy Preston was once a developer way ahead of his time as he is explaining the challenges of Optionals in Swift.

From his lyrics we learn that if you have nothing meaning nil in an Optional you will need to take special care otherwise some operations could crash your program. Swift protects you from this with a warning that you need to unwrap an Optional and check for nil because as Billy Preston says you gotta have somethin’.

This is usually accomplished using an IF LET statement which basically says if the attribute has something you can be with me otherwise return nil and go to the next verse in the song. Of course checking for nil every time can become as monotonous as repeating lyrics in every verse so Swift does allow you to implicitly unwrap an attribute by using an exclamation point at the end of it. This says that you will take the chance that the attribute always has a value. This of course can crash your program if it does equal nil.

Like Billy Preston warns, “I’m not trying to be your hero ‘cause that zero is too cold for me, brrr” so you should always be on the safe side and assume that your optional attributes could be nil and protect yourself with either an IF LET or a GUARD LET. That will make sure that “you gotta have something’ if you want to be with me”. So get your groove on and code safely.

Share this Article

Posted by Jeff Summers

Author, technologist, and baseball aficionado specializing in information technology and developing new and creative ways to interact with the world around us. My goal is to extend the boundaries of what is possible and find ways to make the world a better place while having fun.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Stay Connected

Search

More Articles

Pace Layered Architecture Categorization

Change is inevitable. Today’s computing environments are constantly in flux from internal and external forces that put demands on the systems and necessitate changes to maintain its value stream. Technology currency is a constant struggle that organizations must...

TBM, Another Holy Grail?

Technology Business Management or TBM as a term was coined around 2012 although many of the concepts making up this framework have been around for almost as long as there has been IT. In its simplest form, TBM represents the integration of business, technology, and...

Visionary or Dreamer?

Thinking back over your life you’ve met and worked with countless personality types. You have been a part of teams that struggled to complete simple tasks and the work felt meaningless. Perhaps your goal in these times was to just trudge through the mud hoping the...

Archives