Swift Optionals Tutorial – Unwrapping – Guard, If Let, Chaining, Force

The next topic in my series on iOS Interview questions explains unwrapping Optionals in Swift. This iOS interview question was asked in most almost all interviews. In this Swift tutorial I’ll explain if let, guard statements, optional chaining, and force unwrapping, using Xcode.

This is part of a series of videos about iOS interview questions in Swift. Check out the overview where I list and briefly discuss all the topics that are in this series:
https://youtu.be/56ZO6Gg68tw

Optional Chaining Apple Doc
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

Xcode Project Source Code:
https://www.dropbox.com/sh/rnx5gabo6s9ktt0/AAAr63-6HmSVMLq8KowRFxDJa?dl=0

Follow me on Twitter:
https://twitter.com/SeanA0400

Comments

Gal keidar says:

Thanks Sean! Great video. Just try to talk slower 🙂

Jahid Bin Murtaza says:

i have already completed swift 4 documentation…………. now i want to start my demo projenct
plwase hellp me

Piraba Nagkeeran says:

Thanks. It’s clear understanding. You nailed it

Kid Stagram says:

Clear explanation !!

Amir1974 says:

func yearAlbumReleased(name: String) -> Int? {
   if name == “Taylor Swift” { return 2006 }
   if name == “Fearless” { return 2008 }
   if name == “Speak Now” { return 2010 }
   if name == “Red” { return 2012 }
   if name == “1989” { return 2014 }
   return nil 
}

How to use IF LET here ….?

Kishorekumar E K says:

optional map is another way

jacob bullon says:

Thanks for clearing up guard statements, Sean. These are great videos and very easy to follow along with, but I’ll admit I had to turn the speed to 0.75 to keep up!

Justin says:

Clear and to the point!

Mohammad Tahir says:

Expecting more Thanks

Jahid Bin Murtaza says:

i need advance level all tutorial

LebenWerden says:

in line 10 you write if let number = optionalNumber
does it means that let nuber = optinalNumber is a bollean?

Vasu V says:

Hi Sean Allen
I am new to iOS. i want to send a soap request with xml data. i tired lot of examples . but didn’t work . could you help me.
I am developing swift Project. I have to pass Username & Password inputs as xml data (Username & Password are Params)
Thanks in advance

Ken Pierce says:

awesome! I think I finally get the difference between ways to unwrap, nice and concise. I think I’m even getting used to your delivery speed. Kinda starting to like it.

Kristijan Rotim says:

I watched so many videos about optionals, but this one is fantastic 😀 Thank you

Nishant Mendiratta says:

Great Job @Sean! 🙂

Jerome Torresyap says:

Very nice and clean examples. Thumbs up!

Lars Wiik says:

Thanks. This is a quality tutorial. Keep up the good work!

Nasir Shinnx says:

Was never soo clear about all of these concepts. Thank you soo much. Great Job.

Emil Hørlyck says:

Thank you! I think i finally understand how to handle optionals.

John Tam says:

Great Video! Thanks. One question though – do u know why all those objects in the library, like UIButton!, has to be declare as optionals?

Adis Mulabdic says:

How about when u declare variable like this: var number: Int!
this means: “I am sure 100% that number variable will have value?”

Gunjan Goel says:

great tutorial

Luís Costa says:

Here’s one: Let’s say I have a wrapper that contains a boolean called isHeader… How the hell I can write below code without force wrapper var and be conformed with lint good practices

if wrapper?.isHeader == nil || !(wrapper?.isHeader)! { return .zero }

return CGSize(10)

Tried something like
guard let myWrapper = wrapper, myWrapper.isHeader else { return .zero } but after got lint warning Unused Optional Binding Violation: Prefer `!= nil` over `let _ =` (unused_optional_binding)

Anu suggestion?

RochNoure says:

Great video, thank you for sharing!

Tashi Nyima says:

Great tutorial. There is one more technique to unwrap an optional. Nil-Coalescing Operator: let a = b ?? c

rajagopal lagadapati says:

Great video!

Sean Allen says:

Questions about unwrapping optionals? Leave a comment and I’m happy to help!

Bartosz Dolewski says:

How is the operator “??” called then?

venkataro kunchala says:

Hi Can you please tell about the Database topics in real time implementation

Tema Z says:

Hi Sean! What monitor do you have? Thnx!

kanduri vivek says:

Thanks Sean, big fan of your tutorials 🙂

Kalyani ‘s Kitchen says:

Great explanation

Shail Patel says:

keep’em coming. I subbed

Gjermund Gusland Thorsen says:

Force unwrapping you only want to do when you prototype stuff that is new to you in Playground.

Anil Todakar says:

helpful video thank you.

Reade Plunkett says:

Great video! I just found your channel yesterday and have not stopped watching your videos since. They are extremely motivating. Keep it up! 😀

Badri Gs says:

Just subscribed 🙂 Do more videos

Atomic Fragmentation says:

thank you very good performance 🙂

 Write a comment

*

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!