Unlike its predecessors C and Objective-C, Swift has the concept of
Optional types. By default, variables in Swift cannot be set to
nil, unless they are defined as an
Optional type. This is represented syntactically by appending a
? to the end of a type. For example, if you have an assignment,
var s: String = “”, you would change
String?. This will let you set
s = nil.
However, to use a variable with an
Optional type, it must be unwrapped. There are several ways of doing this: with an if-let statement, guard-let statement, or using a
?. operator. The
?. operator, is similar to the
. operator except it checks if the variable is
nil before unwrapping it and continuing with the
str1 is not nil, the
lowercased() method is called on the unwrapped value of
str2 is nil and the method cannot be called, so the expression results in
There is another, slightly more risky way of unwrapping an
Optional and that is the
! operator instead of the
?, which unwraps an Optional without checking if it is
nil. This tells the compiler that you “know” that the variable you are about forcibly unwrap is in fact not
nil. For example, using the code above,
str1!.lowercased() would work just the same as
str1?.lowercased(). However, if the variable is
nil, then a fatal error is thrown, which would be the case if
str2?.lowercased() was changed to
?. is the only operator described here, you can also use
? before subscripting (
[...]) and calling.
Implicitly Unwrapped Optionals
Optional type can also be defined as implicitly-unwrapped, which is represented by appending an
! to the end of a type, for example
String!. This means that a variable of
Optional type will be forcibly unwrapped without the need for the
! operator. Since the variable can still be
nil, this can cause fatal errors to occur if not used properly.
If-let and guard-let
Perhaps one of the most useful statements in Swift is an if-let and its sibling guard-let. As described above, an if-let allows you to unwrap an optional type, if the value is not
nil, and use it in the body of the if-let statement.
The if-let above takes
str, checks if it is
nil, and if it isn’t assigns
unwrappedStr to the value of
str and executes the code inside the curly braces allowing access to
unwrappedStr, otherwise it will execute the
else statement. Notice, that once outside of the if-let block,
unwrappedStr is no longer available.
Now, the guard-let does almost the opposite of an it-let:
In the example above, the guard-let takes the variable
Optional type, checks if it is
nil, and if it isn’t the value of
str is assigned to
unwrappedStr, otherwise the code inside the block is executed. Note the return statement inside of the guard-let: you must either have a
break (or something else that would cause the code to terminate such as a
nil, it can’t be unwrapped and
unwrappedStr cannot be used.
If you have any questions about
Optional types or anything Swift, or if you like what you’ve read and want to know more Swifty things, please leave a comment here or tweet or PM me on Twitter. Happy Swifting!
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!