Force UnWrapping, Optional Chaining , Optional Binding – Swift 3

Before learning optional , it would have been better, if we have gone through Variables in Swift before.

Wrapping , UnWrapping in Swift

Optional values are the fundamental concept in Swift language, if a variable ends with ‘?’ in declaration, then it is an optional value(wrapping). Meaning of that it may or may not contain value for that. Bit confusing? lets see an example

let normalString : String = “variable without optional”

We have declared a constant string variable without optional ‘?’ , while printing below


it writes as “variable without optional“, now let’s see what happens if we use optional ‘?’

let optionalString : String? = “String variable with optional”


It prints as “Optional(“String variable with optional”). Hope that you can see the difference. Yes, Optional ‘?’ wraps the actual value, and if we need actual value of that variable, we need to unwrap it using ‘!’ , 


 Now it prints as “String variable with optional”. I hope that now you got idea wrapping and unwrapping.

Force unwrapping:

Force unwrapping is nothing but an extracting the values from an optional ‘?’ variable . Ok, we aware that force unwrapping using exclamation mark ‘!’ will return a value. But how it will behave if it does not contain a value. Yes, it will make the application crash at run time. let’s see an example for that

var optionalString : String? // not assigning any value to this string data type

print(optionalString!) // here force unwrapping it.

then, the result in a playground is

fatal error: unexpectedly found nil while unwrapping an Optional value

Now we all got idea about wrapping(‘?’)  , unwrapping(‘!’) and now you will have a question, What is the purpose of it? and where can we use it efficiently?. That’s where optional chaining and optional binding comes.

Optional Chaining and Optional Binding:

let’s assume that we don’t have an optional feature in Swift, and I am creating an instance as ‘a’ for the class named ‘A’ like below. But I am not initializing it properly, just declaring it.

class A{

    let: String = "property in class A"    


let a : A

and I am accessing the property called ‘p’ which is there in that class ‘A’


Now the playground gives me error and crashing with error

Optional chaining in Swift 3
Optional chaining in swift3

Playground execution failed: error: MyPlayground.playground:15:7: error: constant ‘a’ used before being initialized

Optional helps us to avoid this error and is playing a key role here. It avoids the app crashing at runtime though we forget to initialize the class object instance. Let’s take the same example again but with optional

class A{

    let: String = "property in class A"    


var a : A?

I am accessing the property called ‘p’ which is there in that class ‘A’


Now the app won’t crash, but it will print as ‘nil’. This feature in Swift is optional chaining. in the same case, if we do force wrapping it print(a!.p) without initialize then the app will crash.

We now have basic knowledge on optional chaining, wrapping, force unwrapping then what is that optional Binding?.

Optional Binding:

Optional Binding is to get the value from an optional variable to normal variable. let’s take a look at this example

var optionalString : String? = "String with '?' optional" // declaring optional string with value not nil

print(optionalString) // printing it without unwrapping , prints as “Optional(“String with \\’?\\’ optional”)\n”

if let getValue = optionalString { // here , we are getting the value from optionalString to getValue , i.e Optional Binding.

    print(getValue) // if value is there , then prints as "String with '?' optional"



print("value is not there in optionalString") // if value is there , then prints as "value is not there in optionalString"


Hope that you have understood Optional Binding, Optional chaining, wrapping ‘?’ , force unwrapping ‘!’ in swift. Please share this tutorial across your circles and put your thoughts in the comment section as well.

Leave a Reply

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