Swift – Optional | Implicit Optional | Optional Binding | Optional Unwrapping

We will learn about Optionals?, their unwrapping, implicitly unwrapped optional!, optional binding and automatic unwrapping.

Normal Optionals :

What : Optionals (normal) are data types, which have value or nothing. ? mark is used to denote optional. Optional means , it may have or may not have any value. think optional(?) as as box (which can contain some thing or nothing) and ! exclamation mark as key of the box. To fetch value from optional var we need operator exclamation mark !

When : when you want to declare a variable, which may or may not have value in future. Suppose you get API response which “may” have person’s first name, middle name and last name. you can simply use optional data type(?) for all three variables.

Declaration : var nameOfYourVariable : Type?

Implicitly Unwrapped Optional :

What : “implicitly unwrapped” optional, meaning that you do not have to unwrap it to access the value.

When : To make sure it must not be nil, and you do not need to deal with unwrapping.

Declaration : var nameOfYourVariable : Type!



Optional Binding : 

What : To find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.

When : Most recommended way to unwrap optionals.

Syntax : 

if let someValue = someOptional{

//…………use this someValue variable


//……it means someOptional is nil, it has no value.




Implicitly Unwrapped Optional : 

What : Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it is useful to remove the need to check and unwrap the optional’s value every time it is accessed, because it can be safely assumed to have a value all of the time.

These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

When : Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter.



89 thoughts on “Swift – Optional | Implicit Optional | Optional Binding | Optional Unwrapping”

  1. Thіs is the perfect site for anyone who wants to undefstand
    this topic. You realize so much its almost haгd tօ argue wiith you (not that I persnally would want to…HaHa).
    You definitely pսt a brand neww spin on a suƅject that has been discᥙѕsed for many
    years. Excwllent stuff, just excellent!

  2. An impressive share! I have just forwarded this onto a
    friend who was conducting a little homework on this.
    And he actually bought me breakfast simply because I found it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending some time to discuss this issue here on your web site.

  3. Excellent post. I was checking constantly this blog and I’m impressed!
    Extremely useful information specially the last part 🙂 I care for such information a lot.

    I was looking for this particular info for a very long time.
    Thank you and good luck.

  4. Hi! I’m at work browsing your blog from my new iphone!
    Just wanted to say I love reading your blog and look forward to all your
    posts! Carry on the excellent work!

  5. Thanks for ones marvelous posting! I genuinely enjoyed reading it, you can be
    a great author.I will make certain to bookmark your blog and definitely will come back someday.
    I want to encourage you to ultimately continue your great job,
    have a nice evening!

  6. This design is stellеr! You obviously know how to kеep a reader entertained.
    Between your wit and your videos, I was almost moved to start my
    own blog (well, almost…HaHa!) Great job. I really lovᥱd whɑt yⲟu had to say, and mօгe than that, hoԝ you preѕented
    it. Too cool!

  7. You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
    It seems too complicated and extremely broad for me.
    I’m looking forward for your next post, I will try to get the hang of it!

  8. Unquestionably believe that which you stated.
    Your favorite justification seemed to be on the net the
    simplest thing to be aware of. I say to you, I definitely get irked while people think about worries that they plainly don’t know about.
    You managed to hit the nail upon the top as well as defined out
    the whole thing without having side-effects , people could take a signal.
    Will likely be back to get more. Thanks

  9. An impressive share! I have just forwarded this onto a friend
    who was conducting a little research on this. And he in fact ordered me dinner because I
    discovered it for him… lol. So let me reword this….
    Thanks for the meal!! But yeah, thanx for spending some time to
    talk about this issue here on your website.

  10. Yоu’ve made some really god points tһere.
    I loⲟкed on thе web foor more info about the issue and founbd ost individuals will go along wіth your views on this site.

  11. Great іtems from you, man. I have take into account your stuff prior to
    and you’re just extremely magnificent. I actualⅼy like what ʏou have obtained here, certainly like ᴡhat yoᥙ’re saying
    and the way whеrein you say it. You’re making it enjoyable and you
    continue to care for to keep it wise. I cant wait to read
    mᥙch mⲟre from you. That is really a tremendous website.

  12. Heya are using WordPress for your site platform? I’m new to the blog world
    but I’m trying to get started and create my own. Do you need any coding expertise to make your
    own blog? Any help would be greatly appreciated!

  13. Hi tthere are uѕing WordPress for yoᥙr blog platform?
    I’m new tto the log ԝorld but I’m trying tօ geet ѕtarted аnd cгeate my ᧐wn. Dⲟ youu require ɑny html coding
    expertise to makе your own blog? Ꭺny help ԝould be гeally appreciated!

  14. Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  15. Thank you so much. I have created this all myself, to learn and earn :), And beleive me no coding expertsie needed here. I am expert iOS Programming developer and Trainer. i am available on skype at alok.upadhyay32

  16. Good post. I learn something new and challenging
    on sites I stumbleupon everyday. It will always be interesting to read
    through articles from other authors and use something from their sites.

Leave a Reply

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