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.
0 Comments