What does optional chaining do in Swift iOS?

3 min 11 sec read

Author: Manish Methani

Consider Optionals as a kind of "parcel". Before you "unwrap" it you won't know if it contains something or not. Optional in Swift is used to handle optional values. Sometimes values may be present, sometimes not. They are indicated using ? (question) mark after optional name.

Syntax :-

Optionals are indicated using ? at the end of variable type.

  /* Optional Integer declaration */ 
 var age: Int?

 /* Optional String declaration */ 
 var name: String?

Now consider this, one with Optional string

var name: String? = "Jazz"

Another one with a Non-Optional string

var name: String = "Jazz"

Though both versions Optional and Non-Optional look same but they are not. An optional String is not a String with some "optional" setting turned on. It's not a special variety of String. A String and an optional String are completely different types.

What is forced unwrapping?

Unwrapping of an optional value can be done by using ! (exclamation) mark at the end of variable type. Consider this example for better understanding of Forced Unwrapping

1) Before Unwrapping :-

import UIKit

var name:String?
name = "Manish"

if name != nil {
    print(name)
}else {
    print("Your Name has nil value")
}

Output :-

Optional("Manish")

2) After Unwrapping:

import UIKit

var name:String?

name = "Manish"

if name != nil {
/* Exclamation mark is added at the end */
    print(name!)
}else {
    print("Your Name has nil value")
}

Output :-

Manish

Automatic Unwrapping

To avoid using "!" mark at every optional variable , you can do this instead ,

var name:String!

Instead of using ? just declare ! mark at the time of declaration of an Optional variable. Simple and easy :)

Example of Automatic unwrapping:

import UIKit

/* Instead of writing ? mark, use ! mark for automatic unwrapping */
var name:String!
name = "Manish"
  if name != nil{
    print(name)
   }
  else 
   {
    print("Your Name has nil value")
   }

Output :-

Manish

Why Optionals ?

Optionals have two use cases:

1) Things that can fail (I was expecting something but I got nothing)

2) Things that are nothing now but might be something later (and vice-versa)

Some examples:

1) Trying to read a file's contents (which normally returns the file's data) but the file doesn't exist in the library

2) Searching for a match in an array but it may or may not returns data.

3) A property which may or may not available like middle name or spouse in a Person class

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python