Overview & Purpose

One of the most confusing thing in Swift is the use of optionals. The term optional is basically used for those variables whose value may or may not be present. Stop . . ..  here i say “ Not Present ” so dont confuse it with the “ Nil ” or “ NULL ”.

In Objective-C “ Nil “ is used for objects where value is present but is not valid , and it does not work for structures , basic C types , or Enumeration values. For these types of values we use NSNotFound class in Objective-C. So this is the reason why there is a concept of optionals in Swift Language because it works for All Types

To understand this here we take an example first declare one String type constant and then we will convert into Int value.The type String in Swift has built in initializer which convert String to Int value. but we know some string can be convert into integer values but some not

“1711” can be converted into numeric value as 1711 but “Welcome to Optionals” cannot have any obvious numeric value. so it will return nil (In Swift nil means no value).

So here “ Int (numericValueInString)  “  can / cannot return value that’s why we will say it as optional Int which is written as Int?. here question mark represents that it is optional there might be a value and might be not.


If we want to set any optional variables as empty or remove its value then we can assign as nil

it will contain a value as Integer 404

by assigning it to nil as

there would not be any value at all

NOTE :  nil cannot be used for non optional variables .In any case if you want to assign variable to nil then you have to make that variable as optional

If we define any optional variable without any default value then its value will automatically be nil.

Forced Unwrapping in If statement

As we check the nil value in Objective-C or other similar languages , we do same here in If statement to check that either it contains any value or not any value by using equal to “==” operator and not equal to “!=” operator.

once we are sure that variable contains any value then we can access it by adding the exclamation mark at the end of variable. The exclamation mark tell us the “I know this optional definitely has a value please use it.” This is known as Forced Unwrapping.If we will not add any exclamation mark then there will be runtime error.

it will print “  Value of convertNumericStringtoInt is 1711 . “

Optional Binding in If/While Statement

Like forced unwrapping ,optional binding is another way to to check the value of  optional variables and use them .

In optional binding first we check the optional variable that either it contains value of not and if it contains a value then we use it by assign that variable to another temporary constants or variable. This can be used with IF and WHILE statements . the syntax can be written as

If you notice here we have not used any exclamation mark on OptionalVariable so our above example can be rewritten as :

it will print “Value of tempNumber is 1711 .”

This “tempNumber”  can be constant and can be variable. and during printing there is no more need for exclamation mark. We can do multiple optional binding within a single IF statement as a comma-separated list of expressions. like below