Swift Optionals - How to Unwrap (real examples)

preview_player
Показать описание

A common iOS Dev interview question is to explain Optionals and how to unwrap them. In this Swift optionals tutorial I'll explain if let, guard statements, optional chaining, and force unwrapping, and nil coalescing.

This tutorial was created using Xcode 14 and iOS 16.

iOS Developer Interview Questions Playlist:

My iOS Dev Courses:

My Closures Video:

Twitter:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Paul Hudson's Hacking With Swift:

Donny Wals - Combine:

Mark Moeyken’s SwiftUI Books:

#swift #softwaredeveloper #iosdeveloper

Timestamps:
0:00 - What are Optionals
0:59 - Unwrapping - if let
2:57 - Unwrapping - guard let
5:37 - Unwrapping - nil coalescing
7:15 - Force Unwrapping
8:37 - Optional Chaining
9:58 - Real Example - if let
11:09 - Real Example - guard let
11:44 - Real Example - nil coalescing
12:15 - Real Example - force unwrap
Рекомендации по теме
Комментарии
Автор

Good stuff! I frequently avoid the simple nesting logic issue at 2:30 by combining the tests in the if let (if let age = user.age, age > 40)

david-holmes
Автор

Been coding for 40 years. Force unwrap is important to me and I use it all the time BC I want to know right when my code logic is invalid to result in an invalid nil. It does me no good to fail silently then later on find out that some variable was nil minutes ago. This is particularly true when designing/debugging. Then you’ll have some confidence that the code is “fully” functioning (does that ever really happen).

kevyyt
Автор

Wow! This is a tough topic for beginners but your delivery was awesome, hands down! Thank you so much for this !!

androspuddy
Автор

Yo you the man, Sean Allen. Thanks for explaining these things in easy nontechnical terms

thomasgarayua
Автор

i agree with you on the force unwrap example. other example might be the IBOutlets.. they already come with a force unwrap from Apple, because yes, you shold have those components. good video again Sean

Niram
Автор

In the calendar code I would not use Calendar.current. I recently had to fix a crash related to this. Create a Calendar manually with the Gregorian identifier or you may get unknown behavior.
The issue is that the user may have a non-Gregorian calendar set

derickito
Автор

Thank you for the great video! Helped me a lot :)
The "cache example" looks really interesting.
Maybe you could do a video about caching?

ayurockt
Автор

there’s stigma against force unwrapping optionals. Don’t be afraid of force unwrapping when you expect a non-nil value at point of accessing the variable. Running if-lets because “you don’t want the app crashing due to an edge case” will bite in future when debugging your code later

if the app crash happens due to unexpected nil that’s a sign you need to fix the code itself; not the variable

nahidislam
Автор

Thanks a lot! Usefull topic for both beginners and advanced. What about unwrap with compactMap and type casting (as?)

andreifokin
Автор

Great explanations as always, Sean!

JasonMitchellAZ
Автор

'm new to Swift and trying to solve some LeetCode problems in Xcode. I realized that I can't debug in Playgrounds, which makes it hard to see what I'm doing. How can I learn Swift effectively without a debugger? Any advice would be appreciated.
Can I use regular projects as my test ground ?

antonignatenko
Автор

@Sean Allen : sir could you create video about restfull API in swift 5.7, thank you

waldifebrianda
Автор

I would love for Rust to be a supported language for Apple development.

finnmonstar
Автор

I’m working with Dates and I see no reason to no force unwrap UNLESS I don’t know what date is coming through.

Make 100% sure you know what date object is coming through and then make it happen.

officialspaceefrain
Автор

Force unwrapping is helpful with UIImage I’ve found. As long as the image asset name matches the string value, It has always worked in my case. Of course ?? is always there, but then you’ll be force unwrapping that default UIImage value regardless…

jaylensmith
Автор

Optional map is also very useful when you want to pass the non-nil value to a function, otherwise keep the nil:
let width = Float(widthTextField.text ?? “”).map { } ?? nil

mgcarland